DateTime的不同行为 [英] Different behaviour with DateTime

查看:98
本文介绍了DateTime的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我观察到一个场景,如果我的系统日期,DateTime.Now.ToString(MM / dd / yyyy)工作正常格式为MM / dd / yyyy(ControlPanel-> Region and Language)。但是,如果我将系统日期格式更改为dd-MM-yyyy,则DateTime.Now.ToString(MM / dd / yyyy)将以dd-MM-yyyy的形式返回结果。



这种不同场景的任何原因以及我们如何解决这个问题。我希望不管任何系统日期格式我的DateTime.Now.ToString()应该返回与我想要返回的格式相同的结果。



见以下示例。

1)当系统日期格式为MM / dd / yyyy时(ControlPanel->区域和语言)

DateTime.Now.ToString(MM / dd月/年);以预期的正确格式返回日期。



1)当我更改系统日期格式为dd-MM-yyyy(ControlPanel->区域和语言)

DateTime.Now.ToString(" MM / dd / yyyy");以dd-MM-yyyy格式返回日期,尽管指定了ToString(MM / dd / yyyy)

所以我的问题是为什么如果我改变系统日期格式这个行为会改变。





请帮助

先谢谢

Hi Guys,

I have observed a scenario where DateTime.Now.ToString("MM/dd/yyyy") is working very fine if my System Date Format is MM/dd/yyyy(ControlPanel->Region and Language). But if I change the System Date Format to dd-MM-yyyy then DateTime.Now.ToString("MM/dd/yyyy") returning result in the form of dd-MM-yyyy.

Any reason of this different scenario and how can we solve this. I want that irrespect of any System Date Format my DateTime.Now.ToString("") should return the same result of what format I am trying to return.

See below example.
1)When System Date format is MM/dd/yyyy(ControlPanel->Regional and Language)
DateTime.Now.ToString("MM/dd/yyyy"); returning the date in proper format as expected.

1)When I change System Date format is dd-MM-yyyy(ControlPanel->Regional and Language)
DateTime.Now.ToString("MM/dd/yyyy"); returning the date in dd-MM-yyyy format, in spite of specifying the ToString("MM/dd/yyyy")
So my question is why this behavior change if I am changing the System Date format.


Please help
Thanks in Advance

推荐答案

你关于'/'被改为' - '的最后评论是正确的!在日期格式中,斜杠(/)只是日期分隔符<的占位符/ a> [ ^ 。使用反斜杠(\)以您的格式转义斜杠:

Your last comment about '/' being changed to '-' was correct! In date format slash (/) is just a placeholder for date separator[^]. Use backslash (\) to escape the slashes inyour format:
DateTime.Now.ToString("MM\\/dd\\/yyyy");



编辑 - 指定文化

另外,您可以指定文化并使用标准格式:


Edit - specify culture
Alternativelly you can specify culture and use standard format:

DateTime.Now.ToString("d", CultureInfo.InvariantCulture);






请点击此链接: http://stackoverflow.com/questions/7577389/how-to-elegantly-deal-with-timezones [ ^ ]


这篇关于DateTime的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆