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开发者_Python百科 to accept date in my custom format?
Thanks in advance.
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.
MyDateField - DateTime as a string
Format. - "dd/MM/yyyy HH:mm:ss" (for example)
Culture - "EN-US"
So when you read your date from your DataSet to instantiate a DateTime object you then use.
var dateTime = DateTime.ParseExact([mydatefield], [format], new cultureInfo([culture]));
*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.
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.
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:
string formatted = String.Format("{0:yyyy/MM/dd HH:mm}", datetime);
Or if you are getting system date I believe it would work like this:
string formatted = String.Format("{0:yyyy/MM/dd HH:mm}", DateTime.Now);
Or if you are reading a string into a DateTime object, you'd have to do some parsing:
string date = "2011/5/20"
string dateparts = date.Split('/');
string year = dateparts[0];
string month = dateparts[1];
string day = dateparts[2];
DateTime dt = new DateTime(year, month, day, 0, 0, 0);
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
DateTime s = DateTime.Now;
MessageBox.Show(s.ToString("yyddMM"));
MessageBox.Show(s.ToShortDateString());
DateTime s2 = DateTime.ParseExact("110307", "yyddMM", CultureInfo.InvariantCulture);
MessageBox.Show(s2.ToShortDateString());
And I totally agree with Justin here.. As long as you store the date in a text file, just use a string field instead
精彩评论