如何将此字符串转换为Datetime +请参阅此日期时间与当前时间之间的差异

时间:2011-01-17 14:18:38

标签: .net vb.net datetime

嘿,希望将此字符串转换为日期时间

20101119T162837

然后还有如何将这个字符串与当前时间进行比较,我可以说,即:

30分钟差异或2小时10分钟差异等

2 个答案:

答案 0 :(得分:2)

使用DateTime.ParseExact()...

var strValue = "20101119T162837";

var dateValue = DateTime.ParseExact(strValue, "yyyyMMdd'T'HHmmss", CultureInfo.CurrentCulture);
var diff = dateValue - DateTime.Now;
Console.WriteLine("difference: {0:HH}h{0:mm}", diff);

编辑:我不确定默认格式,所以你可以像George Stocker推荐的那样尝试简单的Parse方法

答案 1 :(得分:0)

Guillaume86的Visual Basic代码

    Dim strValue As String = "20101119T162837"

    Dim dateValue As DateTime = DateTime.ParseExact(strValue, "yyyyMMdd'T'HHmmss", System.Globalization.CultureInfo.CurrentCulture)
    Dim diff As TimeSpan = dateValue - DateTime.Now