Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

c#-How to access DateTime?

I am developing a window application in c#.I used DateTimePicker control in the application with MM/DD/yyyy
formate to show the date, but when i deploy my application to other machine it is converting the formate of DateTime in to DD/MM/yyyy automaticaly and giving error message.
I gussed a reason:
Is it due to regional setting for date in different system or any programming error?
if it is so how we can develop an application with portability across the machine's regional setting?
Please help me.
looking forward for reply...

Sandeep Tikariha
Wednesday, May 12, 2004

Well, a DateTime is a DateTime, regardless of how it's converted to a string and output to the screen.

How are you storing/retrieving the DateTime, and how are you getting it to the user?

Greg Hurlman
Wednesday, May 12, 2004

Thanks for reply.
My actual problem is like this:
In my application I am taking a field Birthdate . I am taking DateTime DataType in the database to store the value of Birthdate.I am not taking the field as a string..At front end I am using datetime picker control with format property MM/DD/YYYY, but when I am giving value through form , it is converting the format in database in DD/MM/YYYY.

eg. I am giving 12/5/2004 at client side(in the datetime picker control) and after submitting the date value is stored as 5/12/2004 in database.

sandeep tikariha
Thursday, May 13, 2004

As long as you are not converting to a string at any point yoiu should be ok. I suspect that the application you're using to read the data from the db (Enterprise manager, Query Analyzer?) is just presenting the date to you differently. ie the value is correct - it's just using different formatting to what you expect.

Duncan Smart
Thursday, May 13, 2004

I agree with Duncan - are you seeing incorrect results in your application, or have you just noticed this in your debugging?

Greg Hurlman
Thursday, May 13, 2004

I guess you're using DateTime.Parse and DateTime.ToString to convert to and from string.

To make this conversion locale-independent, use the overloaded version of these methods, that take an IFormatProvider instance, where the IFormatProvider instance that you provide should be constant, e.g. DateTimeFormatInfo.InvariantInfo (which I think chooses USA format as the standard).

Or, instead, I use XmlConvert.ToDateTime and XmlConvert.ToString methods (defined in the System.Xml namespace).

Christopher Wells
Thursday, May 13, 2004

Thanks Duncan & Greg.
What you suspect is right.When I retrive Birth date vaule from db(Sql Server 2000) it is  not giving me the wrong value but exchanging the value of date and month.
This is ok when I give value of Date field less then 12 , but when I give the value greater then 12 e.g 7/20/2003(MM/DD/YYYY) it is converting this value in to 20/7/2003(MM/DD/YYYY) and giving error since the value for a month can not exceed then 12.

Sandeep Tikariha
Thursday, May 13, 2004

*  Recent Topics

*  Fog Creek Home