Forum Moderators: open

Message Too Old, No Replies

Datagrids converting strings to dates

ASP.Net Irritations

         

IanTurner

10:33 am on Apr 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



We are finding that a string value (it is a date in UK format) recovered from a DB Query is being translated into date format (US date) by ASP.Net and is thus displaying incorrectly.

Anyone know why or how we can stop this happening.

edicius

7:05 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



You can do a couple of things...

I'm assuming that your data is stored as a System.DateTime. First thing you should probably do/try is set the CultureInfo property for your application. For English - UK you probably want 'en-GB' (Microsoft has a list of all the available Culture types... a google search on ["CultureInfo Class"] should yield a good list.


C#:
// Creates a DateTime with the Gregorian date January 1, 2004 (year=2004, month=1, day=1).
// The Gregorian calendar is the default calendar for the en-GB culture.
DateTime myDT = new DateTime( 2004, 1, 1 );

The next step is determining the format you would like to use for your DateTime values. You can either do this manually with a sepcific format mask, or by specifying one of the predefined format types:


C#:
//Using a format mask
Response.Write(myDT.ToString("yyyy-MM-dd"));
Response.Write(myDT.ToString("MM-dd-yyyy hh:mm:ss tt"));


//Using a predefined format type
Response.Write(myDT.ToString("d"));
Response.Write(myDT.ToShortDateString());

Using the specific format mask you can format the date however you like. Doing a google search on ["DateTimeFormatInfo Class"] should return a good list (the MSDN list is great).

Using a predefined format type takes into account the CultureInfo you set for your application. The same search above on DateTimeFormatInfo Class will give you information on how these formats will output (the example given via MSDN is uses the EN-US format).

IanTurner

10:18 pm on Apr 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The data was stored as a String only, date values are valid, but so are "Not Yet Available" and "December 04" - it was just that the grid converted the string when only dates were returned by the query.

If the data is stored in date format we don't have a problem getting it in UK format.

aspdaddy

10:14 am on Apr 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to be safe I always explicitly convert dates to a prefered format, before using them:
function toUK(str)
dd=day(str)
mon=left(ucase(monthname(month(str))),3)
yyyy=year(str)
toUK = dd & "-" & "-" & mon & "-" & yyyy
end function