简体   繁体   中英

DateTime.ToString() not working as expected

In the screenshot DateTime.ToString() method is being called but the date is not getting formatted in expected format (as seen in Quick Watch widnow). Is something wrong ?

在此处输入图片说明

You are using / as separator in your ToString format. But your current culture seems to has - as date separator. That is why you see the difference. You can pass CultureInfo.InvariantCulture with ToString .

Like:

DateTimeObject.ToString("MM/dd/yyy HHmmss", CultureInfo.InvariantCulture)

DateTime.ToString replaces / with the current date separator and : with the current time separator. You're passing in the format yourself, and it does not match what's in the Region settings.

To use the Region settings, use ToShortDateString() and ToShortTimeString() .

You can use this:

   DateTime.now.ToString("yyyyMMddHHmmss");

or

   DateTime.now.ToString("mm-dd-yyyy");

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