2017-06-13 28 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"); 
相關問題