简体   繁体   中英

C# System.DateTime.Now returns customized time format set in windows

C#System.DateTime.Now给我带时间的日期,但是无论我在Windows calander中设置的是什么,它都会给我格式,即(am)(pm)甚至是(xyz),如何获取标准时间格式(AM / PM),而无论在Windows calander中设置的自定义时间格式?

System.DateTime.Now doesn't have any presentational format, it's the ToString() that formats it.

Like:

System.DateTime.Now.ToString("dd, MM");

check msdn for more info: https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

If you use the DateTime as DateTime type then it will automatically show you the format of your windows. For the custom format, you have to cast it as string

string newdate = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

useful link MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM