如何将日期从未知区域转换为计算机区域? [英] How do I convert a date from an unknown culture to my computers culture?

查看:53
本文介绍了如何将日期从未知区域转换为计算机区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在从供应商的数据库中导入数据.由于我的供应商在国外,因此数据以其国家的日期格式到达.另外,当我从外部供应商处导入时,我想验证他们提供给我的所有数据.我试图使用convert.todatetime(string),但收到预期的"FormatException未处理"错误未将字符串重新识别为有效的DateTime".我真的很困,不了解足够的C#知识,不知道如何解决这个问题.任何人都可以向我指出正确的方向,
欢呼声,
斯图


使用提供的信息,(并感谢那些贡献者),我现在将代码扩展为:

string myTime = DateTime.Parse(pRow[26].ToString()).ToString(CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern); 



pRow [26]的值为"7/30/2008",但我仍在获取

"FormatException未处理
无法将字符串识别为有效的DateTime."

再看一遍,我想知道一旦克服错误,我将得到什么.当我愉快地导入了外国日期格式后,如何将其存储为本地日期?我现在很困惑,我想去做其他事情,但是我不想让这个问题开放并被忽略.

再往前走一步,如果我遍历日期的数据表,运行TryParse选项,计算机将如何知道"3/5/2008"应该是2008年5月3日还是2008年3月5日? .NET为DateTime提供了许多转换方法,Convert.ToDateTime是最简单的.它的工作原理是包装提供更好控制的DateTime.TryParse方法.话虽如此,如果数据采用特定的文化格式,则第一步是尝试给它提供一种文化:Convert.ToString为此具有一个重载: http://msdn.microsoft.com/en-us/library/ms131044 [ http://msdn.microsoft.com/en-us/library/h9b85w22 [ ^ ]-稍后您可以指定要尝试的转换格式范围.


再往前走,如果我遍历日期数据表,运行TryParse选项,计算机将如何知道"2008年3月5日"是2008年5月3日还是2008年3月5日?

一旦获得数据,就将其存储在适当的数据类型中. 日期时间 [ bool 成功= DateTime.TryParse( includedDateString, System.Globalization.CultureInfo(" ), System.Globalization.DateTimeStyles.None, 退出提供日期 );

您可以从 ^ ].

或使用 DateTime.TryParseExact() [ ^ ]与每个供应商的格式都不同.


格式化特定文化的日期和时间 [ cheers,
Stu


Using the information provided, (and thanks to those who have contributed) I have now expanded my code to:

string myTime = DateTime.Parse(pRow[26].ToString()).ToString(CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern); 



The value of pRow[26] is "7/30/2008" but I''m still getting

"FormatException was Unhandled
String was not recognized as a valid DateTime."

And looking over this again, I''m wondering what I''m going to get once I get past the errors. Once I''ve happily imported the foreign date format, how do I then store it as my local date? I''m now so confused I want to go and work on something else, but I don''t want to leave the question open and ignored.

And going one step further, if I''m looping through a datatable of dates, running the TryParse option, how will the computer know if "3/5/2008" should be 3rd May 2008 or 5th March 2008?

解决方案

.NET provides a lot of conversion methods for DateTime, Convert.ToDateTime is the simplest. How it works is to wrap the DateTime.TryParse method which provides finer control. Having said that, the first stage is to try giving it a Culture, if the data is in a specific cultural format: Convert.ToString has an overload for that: http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx[^]

If that doesn''t help, and the data is always in a specific format, then DateTime.TryParseExact provides culture and format specific methods which should be able to decipher the date info: http://msdn.microsoft.com/en-us/library/ms131044[^] and http://msdn.microsoft.com/en-us/library/h9b85w22[^] - the later of which allows you to specific a range of conversion formats to try.


And going one step further, if I''m looping through a datatable of dates, running the TryParse option, how will the computer know if "3/5/2008" should be 3rd May 2008 or 5th March 2008?

Once you got the data, you will store it in an appropriate data type. DateTime[^] will do in your software, some similar data type should exist in every DBMS. Or, at least, you formatted the data. So you will know what it means.

To get there, provide a culture for every supplier.

bool success = DateTime.TryParse(
    suppliedDateString,
    new System.Globalization.CultureInfo("fr-FR"),
    System.Globalization.DateTimeStyles.None,
    out suppliedDate
);

You can get the culture identification strings ("fr-FR") from here[^].

Or use DateTime.TryParseExact()[^] with different formatting for every supplier.


How to convert a Datetime string to a current culture datetime string[^]

Formatting Date and Time for a Specific Culture[^]


这篇关于如何将日期从未知区域转换为计算机区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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