C#ConvertTimeFromUtc funkiness

时间:2017-01-24 04:20:07

标签: c#

使用下面的代码,我尝试将此datetime字符串转换为本地DateTime

private DateTime ConvertToLocalTime(string datetimestring)
{
    DateTime timeUtc = DateTime.Parse(datetimestring);
    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
    return cstTime;
}

这是我得到的例外情况:

  

at System.TimeZoneInfo.ConvertTime(DateTime dateTime,TimeZoneInfo sourceTimeZone,TimeZoneInfo destinationTimeZone,TimeZoneInfoOptions flags,CachedData cachedData)          在System.TimeZoneInfo.ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo destinationTimeZone)

     

无法完成转换,因为提供的DateTime没有Kind       属性设置正确。例如,当Kind属性为DateTimeKind.Local时,       源时区必须是TimeZoneInfo.Local。

ConvertTimeFromUtc的示例与我的代码完全相同,只是我将此字符串解析为timeUtc: 2017-01-23T05:00:00+00:00

如果我像这样打电话给Parse:

DateTime.Parse(datetimestring, null, System.Globalization.DateTimeStyles.RoundtripKind);

timeUtc.Kind.ToString()返回"本地"

那么,我该如何解决这个问题呢?时间将发送给我UTC。

3 个答案:

答案 0 :(得分:3)

implementation converts结果进入本地时间,无论输入字符串中指定的区域如何。您必须明确指定您想要UTC结果,因为Sub PassFailValidation() Dim cl As Range, Dic As Object Set Dic = CreateObject("Scripting.Dictionary"): Dic.CompareMode = vbTextCompare With Sheets("Latency") For Each cl In .Range("B2:B" & .Cells(.Rows.count, "C").End(xlUp).Row) Dic.Item(cl.Value) = cl.Row Next cl End With With Sheets("Test") For Each cl In .Range("A2:A" & .Cells(.Rows.count, "A").End(xlUp).Row) If Dic.Exists(cl.Value) Then Sheets("Latency").Cells(Dic(cl.Value), 15) = cl.Offset(, 1) '<--| write the values Dic.Remove (cl.Value) End If Next cl End With End Sub 要求DateTime值为UTC类型。

DateTime.Parse

答案 1 :(得分:1)

您必须添加DateTime.SpecifyKind

private static DateTime ConvertToLocalTime(string datetimestring)
{
    DateTime timeUtc = DateTime.Parse(datetimestring);
    var dt = DateTime.SpecifyKind(timeUtc, DateTimeKind.Utc);
    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(dt, cstZone);
    return cstTime;
}

.net Fiddle

答案 2 :(得分:0)

如果您只想转换为本地日期时间并且您不需要偏移量,因为您的字符串具有偏移信息,function dress(){ var blousePrice = document.getElementById('blouseDbPrice').value; var tShirtPrice = document.getElementById('tShirtPrice').value; if(document.getElementById('d1').checked) { price += blousePrice; } else if(document.getElementById('d2').checked) { price += tShirtPrice ; } } 将使用偏移信息并转换为本地日期时间。那么你需要做的就是:

DateTime.Parse

如果您需要带有偏移信息的本地日期时间,请查看DateTimeOffset,因为它是出于这个目的:

private static DateTime ConvertToLocalTime(string datetimestring)
{
    // Parses to local datetime
    // check the Kind property and you will see it has Local
    return DateTime.Parse(datetimestring);
}

如果您需要将utc日期时间转换为其他时区而非本地时区,请按以下方式指定:

private static DateTimeOffset ConvertToLocalTime(string datetimestring)
{
    DateTime timeUtc = DateTime.Parse(datetimestring, null, DateTimeStyles.AdjustToUniversal);
    DateTimeOffset dateCst = new DateTimeOffset(timeUtc, TimeZoneInfo.Local.BaseUtcOffset);

    return dateCst;
}

这是文档:

private static DateTimeOffset ConvertToLocalTime(string datetimestring)
{
    DateTime timeUtc = DateTime.Parse(datetimestring, null, DateTimeStyles.AdjustToUniversal);
    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTimeOffset dateCst = new DateTimeOffset(timeUtc, cstZone.GetUtcOffset(timeUtc));

    return dateCst;
}