不同文化的日期时间转换
我想知道转换日期时间的最佳方法是什么? 现在我的计算机设置为dd/mm/yyyy
,但我将日期设置为mm/dd/yyy
,所以当我尝试这样做时,
DateTime.Convert();
DateTime.Parse();
DateTime.TryParse();
我要么什么也没有得到,要么它崩溃。现在我可以告诉它 mm/dd/yyyy
格式,它可能会转换。但问题是这些日期来自用户上传的文件。
它们可以是任何格式的组合。我正在努力寻找方法来做到这一点。
我看到的问题是我正在查看上传文件中的日期,因此我不确定查看浏览器日期格式是否有帮助。
我正在使用 asp.net mvc,我试图找到一个自动处理日期格式的解决方案,这样我就不必询问用户他们拥有的日期格式(我认为我必须问他们的事情越多,可能性就越小用户将继续)
I am wondering what is the best way to figure out how to convert a datetime?
Right now my computer is setup as dd/mm/yyyy
yet I am taking date as mm/dd/yyy
so when I try to do
DateTime.Convert();
DateTime.Parse();
DateTime.TryParse();
I either get nothing back or it crashes. Now I could tell it about the mm/dd/yyyy
format and it probably would convert. However the problem is these dates are are coming from user uploaded files.
They could be in any format combination. I am trying to find ways to do it.
The problem I see is that I am looking at the dates in an uploaded file so I am not sure if looking say at the browser date format would help or not.
I am using asp.net mvc and I am trying to get a solution that handle date formats automatically so I don't have to ask the user about the date format they have (I figure the more things I have to ask them the less likely the user will continue on)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,一旦该值位于服务器上,您就无法自动找出用户打算使用的日期时间格式。您需要更多信息才能正确解析它(例如,1/2/3 可能意味着许多不同的日期,具体取决于文化)。
请考虑以下解决方案之一:
d.getUTCFullYear()+"-" + d.getUTCMonth() + "-" + d.getUTCDate()
。No, you can't figure out automatically what date-time format a user meant to use once the value is on the server. You need more information to parse it correctly (e.g. 1/2/3 can mean a lot of different dates depending on the culture).
Consider one of the following solutions:
d.getUTCFullYear()+"-" + d.getUTCMonth() + "-" + d.getUTCDate()
.chobo2(我喜欢“句柄”):)
你可以检测语言环境并随意处理。请参阅以下 SO Q/A 以获得指导:
系统在哪里.Net 的语言环境/文化设置
关键是不必特别设置任何内容,而是识别语言环境并采取相应的操作。
chobo2 (I like the 'handle') :)
you can detect the locale culture and work on that at will. see the following SO Q/A for pointers:
Where is the system locale/culture set for .Net
the key is to NOT have to set anything in particular, but identify the locale and act accordingly.