Datetime.Parse

andycharger

Well-known member
Joined
Apr 2, 2003
Messages
152
I am having a problem with dates (still)
I have a date 0f 1st April 2003
In the UK (where I am) it would read as "01/04/2003"

I am parsing my string before sending it in a SQL statment like this

Code:
strDate = DateTime.Parse(Request.Form("txtDateRec"))

The problem is that this is converting it in the database to "04/01/2003" (american format)

What do I need to do in my parse command to ensure it goes in my DB as "01/04/2003"????

Help please!!!
 
try
Code:
Dim ci As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")

Dim d As DateTime = DateTime.Parse("01/04/2003", ci)
 
Hi PLausibly,
I tried that but it still doe not change the formatting. I stepped into the code and looked at the variables. They still appear as #11/1/2003# when im trying to put in 1st November 2003 (01/11/2003)

Any more ideas mate?
 
Whoops - only read half your question

Code:
        Dim ci As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")

        Dim d As DateTime = DateTime.Parse("01/04/2003", ci)
        Dim s As String
        s = d.ToLongDateString()            May work
        s = d.ToString(ci.DateTimeFormat)   Should work
 
PLausibly,
This still is not working. I watched the locals and left the code exactly as you entered it. In the locals, it converts the date to the following:
#4/1/2004#
Whereas it should say "01/04/2004"

Is the ci bit doing anything? What imports do I need in the project to ensure these are all there?
 
Code:
s = d.ToString("dd/MM/yyyy")

although the bit
Code:
 s = d.ToString(ci.DateTimeFormat)
formatted okay for me - did include the time portion though.
 
Neither of those are formatting my date at all. No matter what I change the specificculture to. I therefore think the culture is not being picked up.

In fact, I have done both of these to force it
Code:
d = DateTime.Parse("01/08/2004",ci)
strDateRec = d.ToString("dd/mm/yyyy")

d = DateTime.Parse("01/08/2004",ci)
strDateRec  = d.ToString(ci.DateTimeFormat)

the first one puts outputs the following in the watch window "01/00/2004"
obviously that fails on the insert.

can you confirm what imports I need to put at the top of my page or in my project?
 
What database are you using and what tool are you using to display the date from the database?

Have you considered a format of
strDateRec = d.ToString("dd/mmm/yyyy")
 
thanks all (particularly Plausibly).
It was formatting correctly in .net but my DB was screwing with it. Had to change the user regional settings to British English! Doh!

All fixed!
 


Write your reply...
Back
Top