.Net DataTable日期问题
我正在开发一个项目,但没有任何数据库。我正在使用 ADO.Net 的数据集,然后为了存储我的数据,我编写 xml 文件并读取 xml 文件并再次加载数据集。 xml 文件是项目文件,我必须将此文件从一个系统移动到另一个系统。
在这个数据集中,还有一个 DateTime 列。
我想以特定格式( dd/MM/yyyy )将日期存储在字段中。
目前,我的问题是,它检测系统的日期格式并读取日期。但系统的日期格式可能因系统而异。有些人使用 MM/dd/yyyy,有些人使用 dd/MM/yyyy。因此,当格式更改时,它会引发异常。他们有什么解决办法吗?
我可以强制数据表的 DataTime 字段接受我的自定义格式的日期吗?
提前致谢。
I am working on a project and i am not having any database. I am using Dataset of ADO.Net and then to store my data i write the xml file and read xml file and load the dataset again. The xml file is the project file and i have to move this file from system to system.
In this data set, there also a column of DateTime.
I want to store Date in field in a specific format ( dd/MM/yyyy ).
Currently, my problem is that, it detects the system's date format and read the date. But system's date format can vary system to system. Some people use MM/dd/yyyy and some use dd/MM/yyyy. So, when the format is changed, it throws the exceptions. Is their any solution for it?
Can i force DataTime field of data table to accept date in my custom format?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我过去在序列化日期时间方面也遇到过类似的问题。
我建议您删除日期时间字段并使用字符串。这样您就可以完全控制将日期时间对象输入到数据集中的格式。
将其作为字符串输入后,您需要记录记录日期时间的文化和格式。您可以在软件中对这两个值进行硬编码,并希望它们不会改变(不理想)。将它们放到 .config 文件中(更好)。
或者
将这些附加字段添加到数据集中。
因此,当您从 DataSet 读取日期来实例化 DateTime 对象时,然后使用。
*请注意,[] 内的字段是从数据集中的行检索的值。
这样,当您在数据集中指定要使用的区域性和格式提供程序时,您就可以在任何计算机上使用此日期时间字段。
您需要重构从数据集中实例化日期时间字段的所有位置,以使用这些日期时间提供程序和区域性信息。
就我个人而言,我会从 .config 文件中读取格式和区域性信息,因为这些信息不太可能在数据集中逐行更改。
I've had similar issues with serializing date time in the past.
I'd recommend that you drop your datetime field and use a string instead. That way you have full control over what format the date time object is entered into your DataSet.
Once you have it entered in as a string, you need to record what culture and what format the datetime is recorded in. You can either hard code those two values inside your software and hope they don't change (not ideal). Drop those to a .config file (better).
or
You add those additional fields to your dataset.
So when you read your date from your DataSet to instantiate a DateTime object you then use.
*Note that fields inside [] are values retrieved from a row in the dataset.
This way you can use this datetime field on any machine as you are specifying in the dataset what culture and format provider to use.
You will need to refactor everywhere you are instanciating a datetime field from the dataset to use those datetime provider and culture information.
Personally, I'd go with reading the format and culture information from a .config file as these are not likely to change on a row by row basis in your dataset.
你有没有尝试过这个:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
您可以在第二个参数中指定数据的格式。
当您使用 String.format 方法写入日期时,也可以格式化日期。
Have you tried this:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
you can specify the format of the data in the second argument.
The date can also be formatted when you write it with String.format method.
DateTime 对象只是将日期存储为属性,或月、年、日等的一系列属性。但是您可以像这样格式化日期字符串:
或者如果您正在获取系统日期,我相信它会像这样工作:
或者,如果您将字符串读入 DateTime 对象,则必须进行一些解析:
但是 DateTime 对象可以从日期的各种字符串表示形式初始化,通常不需要解析。取决于你的情况,
The DateTime object just stores the date as a property, or a series of properties for Month, Year, Day, etc. But you can format a date string like so:
Or if you are getting system date I believe it would work like this:
Or if you are reading a string into a DateTime object, you'd have to do some parsing:
But the DateTime object can be initialized from a variety of string representations of a date, usually you dont need to parse. Depends on your situation,
只是将日期时间序列化/反序列化为特定格式的一个简单示例
我完全同意贾斯汀的观点..只要将日期存储在文本文件中,只需使用字符串字段即可
Just a quick example for serializing/deserializing a datetime to a specific format
And I totally agree with Justin here.. As long as you store the date in a text file, just use a string field instead