2016-06-07 55 views
0

試圖獲取不同國家(時區)的日期然後顯示它。我收到時間和日期。只需要日期如何從不同的時區C獲取日期部分#

var info = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); 
    var dateTime = TimeZoneInfo.ConvertTime(DateTime.Now.Date, info); 

    MessageBox.Show(dateTime.Date.ToString()); 

極品2015年5月5日,但還不是時候

+0

您是否需要支持全球化,是應用程序在許多國家還是僅用於一個位置。 –

+0

要將DateTime設置爲字符串格式並獲取不同的格式,請檢查ToString文檔,它接受一個指定格式的參數,或者使用'.ToShortDateString()'。 –

+0

MessageBox.Show(dateTime.Date.ToString(「dd/MM/yyyy」)); –

回答

0

您只能使用ToShortDateString()

var info = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); 
var dateTime = TimeZoneInfo.ConvertTime(DateTime.Now.Date, info); 

MessageBox.Show(dateTime.Date.ToShortDateString()); 

得到date或者你也可以直接使用

MessageBox.Show(dateTime.ToShortDateString()); 
+0

如果「當前文化」設置正確,這種方法非常有效。 –

+0

您應該用'DateTime.UtcNow'替換'DateTime.Now.Date' – CodesInChaos

1

您可以直接使用

MessageBox.Show(dateTime.ToShortDateString()); 

,否則你可以自定義

MessageBox.Show(dateTime.ToString("dd/MM/yyyy")); 
0

你需要在任何情況下使用的格式,如果要指定區域性特定國家,你可以做到這一點,或使用的一個

var ci = new CultureInfo("en-US"); 

var ci = CultureInfo.InvariantCulture;類的DateTime(ToShortDateString(),ToLongDateString(),等等)的方法探討的CultureInfo以獲取更多信息

然後使用ToString方法以特定格式獲取您的日期,請參閱該格式不包括小時或分鐘。

dateTime.ToString("MM/dd/yyyy", provider) 
相關問題