如何将2002年1月1日转换为VB.NET日期格式

时间:2015-10-01 14:17:45

标签: vb.net date time format

如何将2002年1月1日转换为VB.NET日期格式

我计划从那天起直到今天在VB.net

由于

2 个答案:

答案 0 :(得分:2)

Date.Parse用于将字符串解析为日期。

然后将其从DateTime.Now移开以查找天数:

MessageBox.Show((DateTime.Now - Date.Parse("01 January 2002")).TotalDays.ToString())

此处不需要DateTime.ParseExact,因为该日期不能含糊不清

答案 1 :(得分:0)

如果您接受来自最终用户的任意文本,则应使用TryParse。如果解析失败,Parse方法将抛出异常。 TryParse没有。相反,如果解析成功,它将设置一个变量并返回True。如果无法解析字符串,则变量不变,返回False。

Dim TempDate As Date
If Date.TryParse("some string", TempDate) Then
    'Work with the TempDate, it was set correctly.
    MessageBox.Show(TempDate.ToString)
Else
    'The string could not be parsed
    MessageBox.Show("The value could not be parsed as a date.")
End If

如果要控制如何尝试解析(例如文化),则需要使用接受IFormatProvider参数的重载。

相关问题