Mr.R Mr.R - 22 days ago 6
Vb.net Question

String to datetime format installdate

how to convert string to date time in listview vb.net
20160928063905.000000+480
cant resolve with Parse

Win32_OperatingSystem
InstallDate

Screenshot

Else
log.BackColor = Drawing.Color.Aqua
log.SubItems(1).Text = client.Machine_Name
log.SubItems(2).Text = client.Network_Information.IPAddress(0)
log.SubItems(3).Text = DateTime.Now.ToShortDateString()
log.SubItems(4).Text = client.Network_Information.Name ' caption
log.SubItems(5).Text = client.Network_Information.InstallDate
log.SubItems(6).Text = client.Network_Information.ID
log.SubItems(7).Text = client.Network_Information.Manufacturer

End If

Next

Dim item As New ListViewItem
item.Text = EventLogListView.Items.Count + 1
item.SubItems.Add(client.Machine_Name)
item.SubItems.Add(client.Network_Information.IPAddress(0))
item.SubItems.Add(DateTime.Now.ToShortDateString())
item.SubItems.Add(client.Network_Information.Name) 'captions
item.SubItems.Add(client.Network_Information.InstallDate)
item.SubItems.Add(client.Network_Information.ID.ToString)
item.SubItems.Add(client.Network_Information.Manufacturer)


item.BackColor = Drawing.Color.Aqua
EventLogListView.Items.Add(item)

Answer

You can use a function like this to change the format of your date before setting the text in your view :

Public Function GetDateFromWin32Date(dateStr As String) As String
        Dim newDateStr = dateStr.Substring(0, dateStr.IndexOf(".", StringComparison.Ordinal))
        Dim newDate = DateTime.ParseExact(newDateStr, "yyyyMMddHHmmss", CultureInfo.InvariantCulture)
        Return newDate.ToString("yyyyMMdd") 'Use the format you'd like to display
End Function

Or in a single line :

log.SubItems(5).Text = DateTime.ParseExact(client.Network_Information.InstallDate.Substring(0, client.Network_Information.InstallDate.IndexOf(".", StringComparison.Ordinal)), "yyyyMMddHHmmss", CultureInfo.InvariantCulture).ToString("yyyyMMdd")

And use it like this :

log.SubItems(5).Text = GetDateFromWin32Date(client.Network_Information.InstallDate)
...
item.SubItems.Add(GetDateFromWin32Date(client.Network_Information.InstallDate))
Comments