在不同文化中解析日期 [英] Parsing date in different culture

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

问题描述

我正在开发应从美国客户使用的软件.
在我的PC(意大利语)上,我使用

I'm developing a software that should be used from a US customer.
On my pc (italian) I use

CultureInfo cultureUS = CultureInfo.GetCultureInfo("en-US");
DateTime dt = DateTime.Parse(s, cultureUS);

,它可以与 2012年9月8日星期六这样的字符串一起使用.
但是当我的软件在客户PC上使用时,他得到了错误

and this works with a string like Sat, Sep 8, 2012.
But when my software is used on customer pc, he gets the error

字符串未被识别为有效的DateTime

String was not recognized as a valid DateTime

为什么?怎么了?
我应该用什么使它在任何地方都能正常工作?

Why? What's wrong?
What should I use to let it work everywhere?


只是为了避免造成混淆:我从网络和文件中读取了这类日期,我需要解析它们,然后使用它们(以这种格式)编写其他文件.
所以我以为我不需要将它们转换为标准"格式,然后再次将它们转换回:我想从该格式使用它们并将它们直接写到文件中...
这就是我决定使用我编写的代码的原因....


just to avoid confusion: I read those kind of dates both from web and files and I need to parse them and then use them (in that format) to write some other file.
So I thought I did not need to convert them to a "standard" format and then convert them back again: I'd like to use them from that format and write them directly to files...
And this is the reason I decided to use the code I wrote....

推荐答案

我不习惯本地化,但是也许这对您有用:

I'm not used to localizations but perhaps this might work for you:

DateTime.Parse(date, new DateTimeFormatInfo()
                         { LongDatePattern = "ddd, MMM dd, YYYY" });

对于我以您的字符串格式向其抛出的字符串来说,它似乎很适合我,但是我没有用不同的文化对其进行测试.

It appears to be working for me for the strings I've thrown at it in your string format, but I haven't tested it with varying cultures.

这篇关于在不同文化中解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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