2017-06-13 64 views
1

我从我的JavaScript客户端生成日期时间值到UTC格式我需要能够将此日期时间值转换为特定文化的日期和时间。基于cultureinfo提供的通用日期时间转换为datetime

我这样做是为了获得日期时间

new Date().toUTCString(); // "Tue, 13 Jun 2017 07:44:58 GMT" 

在我的C#控制台应用程序

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK"); 
var dt = DateTime.Parse("Tue, 13 Jun 2017 07:44:58 GMT", Thread.CurrentThread.CurrentCulture); 
Console.WriteLine(dt); 

我总是显示在我区的时间而不是日期时间CultureInfo的值,我传递给它。

我需要的是,当我解析具有特定文化的世界时,就是向我展示特定cultureinfo(上述代码中的丹麦语)的日期和时间。我如何去做这件事?

+0

嗯,可能是寻找多一点,这个问题被问了很多次。 –

+3

'CultureInfo'与时区无关。简单的例子:“en-US” - 您希望美国的多个时区中的哪一个可以参考? –

+3

我会避开那种文本格式 - 我强烈建议将值设置为ISO-8601字符串的格式,或者甚至只是从JavaScript中传递毫秒以来的unix-epoch值。 –

回答

0

尝试

// See - https://stackoverflow.com/a/7908482/1603275 for a fuller list of options 
TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); 

// Not sure if DateTime.UtcNow will default to DateTimeKind.Utc 
DateTime utcDate = DateTime.UtcNow; 
utcDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc); 

DateTime localDate = TimeZoneInfo.ConvertTimeFromUtc(utcDate, sourceTimeZone); 
Console.WriteLine(localDate); 
0

它可以帮助全

 System.Globalization.CultureInfo customCulture = new System.Globalization.CultureInfo("da-DK", true); 
     //to change the date time patern 
     //customCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd h:mm tt"; 

     System.Threading.Thread.CurrentThread.CurrentCulture = customCulture; 
     System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture; 

     DateTime newDate = System.Convert.ToDateTime("Tue, 13 Jun 2017 07:44:58 GMT");