簡體   English   中英

如何在dateTime中轉換字符串日期?

[英]How to convert string date in dateTime?

我有字符串格式的日期。 我想以以下格式轉換日期時間。 我已經介紹了幾個鏈接,但未獲得確切的輸出。

String date = "03-23-16"; //MM-dd-yy

要求:日期格式應為“ 2016年3月23日”

有人可以建議我如何轉換嗎?

你可以將其轉換為DateTime使用,也就是說, DateTime.ParseExact然后將其轉換回string使用格式"MMMM dd, yyyy"

String date = "03-23-16"; //MM-dd-yy note that MM here
DateTime dtInstance = DateTime.ParseExact(date, "MM-dd-yy", null); //this is how you convert string to DateTime
string newDate = dtInstance.ToString("MMMM dd, yyyy"); //this is how you convert it back with format as you want

另外,請注意, mmC# DateTime 分鍾格式,而MMmonths

嘗試這個:

string date = "01-08-2008";
DateTime dt = DateTime.ParseExact("24/01/2013", "mm-dd-yy", CultureInfo.InvariantCulture);

解析為字符串:

dt.ToString("MMMM dd, yyyy");
DateTime.ParseExact("03-23-16", "MM-dd-yy", null).ToString("MMMM dd, yyyy")

您可以使用ToLOngDateString()將其轉換為長日期格式

 string date = "03-05-16";
 date = date.Replace('-', '/');
 DateTime dt = Convert.ToDateTime(date);
 string newDate = dt.ToLongDateString();

它將打印:2016年3月3日。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM