字符串到日期时间格式installdate

时间:2016-11-14 09:39:46

标签: .net vb.net

如何在listview vb.net中将字符串转换为日期时间 20160928063905.000000 + 480 无法解决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)

2 个答案:

答案 0 :(得分:1)

在视图中设置文本之前,您可以使用此类函数更改日期格式:

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

或者在一行中:

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

并像这样使用它:

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

答案 1 :(得分:1)

.NET中的System.Management命名空间包含一种转换WMI编码日期的方法。

Dim installDT = ManagementDateTimeConverter.ToDateTime(wmiDateString)

给定一个"20160928063905.000000+480"字符串,我的系统上的结果为:

  

2016年9月27日17:39:05.000

提取字符串数据会产生不同的值:

  

2016年9月28日06:39:05.000

错误的原因是因为在第一步中丢弃了偏移信息:

dateStr.Substring(0, dateStr.IndexOf(".", StringComparison.Ordinal)