Convert string to datetime Using C#

2020-01-29 16:06发布

How can i convert String like 20100102 into datetime in a formate of dd/MM/yyyy?

5条回答
Animai°情兽
2楼-- · 2020-01-29 16:41
var userdateformat = DateTime.ParseExact("20101020", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Modify as you want to modify.

查看更多
\"骚年 ilove
3楼-- · 2020-01-29 16:46
var result = DateTime.ParseExact("20100102", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Modify as needed.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-29 16:47
IFormatProvider culture = new CultureInfo("en-EN", false); // use your culture info
DateTime dt = DateTime.ParseExact(myDateTimeString, "yyyyMMdd", culture, DateTimeStyles.NoCurrentDateDefault); 

yyyyMMdd is input format here.

And then if you wish convert it to string:

String output = String.Format("{0:dd/MM/yyyy}", dt);
查看更多
够拽才男人
5楼-- · 2020-01-29 16:52

You can use DateTime.ParseExact method

查看更多
霸刀☆藐视天下
6楼-- · 2020-01-29 16:52
string strStartDateMain = "20100102";
string strStartDateFinal = new DateTime(Convert.ToInt32strStartDateMain.Substring(0, 4)), Convert.ToInt32(strStartDateMain.Substring(4, 2)), Convert.ToInt32(strStartDateMain.Substring(6))).ToString("dd/MM/yyyy");
查看更多
登录 后发表回答