简体   繁体   English

DateTime.ToString()不能按预期工作

[英]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). 在屏幕快照中,调用了DateTime.ToString()方法,但日期未按预期格式格式化(如“快速观察” widnow中所示)。 Is something wrong ? 有什么事吗

在此处输入图片说明

You are using / as separator in your ToString format. 您正在使用/作为ToString格式的分隔符。 But your current culture seems to has - as date separator. 但是您当前的文化似乎具有-作为日期分隔符。 That is why you see the difference. 这就是为什么您会看到差异。 You can pass CultureInfo.InvariantCulture with ToString . 您可以通过ToString传递CultureInfo.InvariantCulture

Like: 喜欢:

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

DateTime.ToString replaces / with the current date separator and : with the current time separator. DateTime.ToString用当前日期分隔符替换/ ,用当前时间分隔符替换: 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() . 要使用Region设置,请使用ToShortDateString()ToShortTimeString()

You can use this: 您可以使用此:

   DateTime.now.ToString("yyyyMMddHHmmss");

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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