Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date time format

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I have some code to do with dates that I have been using for while with my page set to "Culture=en-GB". I now need an alternative version of this page with dates in American format MM/DD/YYYY rather than British format DD/MM/YYYY.

I have changed the Culture on the page to en-US, and I am building my date like this :

DateFrom = Convert.ToDateTime(MonthFrom.SelectedItem.Value + "/" + DayFrom.SelectedItem.Value + "/" + YearFrom.SelectedItem.Value);

I then convert the date to my required format (e.g. 15th January 2003 = 1/15/2003) :

DateFrom = Convert.ToDateTime(DateFrom.ToString("d"));

This line worked when the page culture was set to en-GB, but it doesn't work with the page culture set to en-US. Instead of 1/15/2003, I get 1/15/2003 12:00:00 AM.

Is there a different way to format dates in en-US? That would be the only explanation for why this doesn't work.



Any help would be really appreciated.

Thanks,

Mike
 
Are you trying to avoid the addition of the 12:00:00AM to the end of your date? If so, just try this (there probably is a better way, but this does work).

string[] parsed = new string[3];
char[] space = new char[1];
space[0] = ' ';

parsed = YourDate.Split(space, 3);

YourDate = parsed[0];

Assuming YourDate is a string (just convert whatever you have to a string if not), this will break it down into three parts, the actual date, the time, and AM or PM. Just use whatever you want out of the array. I set YourDate = to the first array value, which would be 1/15/2003 in your example. In some formats, there isn't a space between the time and AM or PM. In that case, just declare parsed as 2 in length, and use 2 instead of 3 in the Split call. In that case you would have a date and a time as your two array values. Hope this helps.

-Andrew R.
 
Code:
DateFrom = Convert.ToDateTime(DateFrom.ToString("d"));
There is another overload for the ToDateTime method that takes a string (which is what you have) plus an object that implements IFormatProvider, which is what the CultureInfo class provides.

So if you were to create a new CultureInfo object for the US, you could pass it to this method and get US-formatted dates out.
Code:
CultureInfo USCulture = New CultureInfo("en-US");
DateFrom = Convert.ToDateTime(DateFrom.ToString("d"), USCulture);
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
When working with dates I prefer working with CultureInfo class. But for your problem you should try .ToShortDateString() or .ToString("d", Null)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top