views:

104

answers:

7

Hi folks,

I seem to have a date formatting problem every day!

I am querying a table and am getting a date back in the format dd/mm/yyyy (as a string btw). Brilliant! thats what I want. But, now I want to convert that string to a date so i can do

dim dayNumber as integer = day.DayOfWeek 

But when I convert it to a date it changes it to #m/dd/yyyy#. AHHHH! how can I change this?

here is my code i've tried

Dim ActivityDate As String
If dt.Rows(i)("Date") Is DBNull.Value Then
    ActivityDate = ""
Else
    ActivityDate = dt.Rows(i)("Date")
End If

Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse(ActivityDate, ci)

Dim day As Integer = theDate.DayOfWeek

Cheers

A: 

Are you parsing it like this:

Dim newDate as DateTime = DateTime.Parse(myDate)
Josh
I wasn't no. Tried that and it was just the same.
iamjonesy
A: 

If the culture of your system does not use that date format, then you should get that date string as an actual date:

' canadian date format is dd/mm/yyyy 
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse("13/04/2010", ci)
ScottE
I've retrieved the date as string. So will be converting from string to date. I tried your example but it never changed the formatting :(I've added the code to my post
iamjonesy
+1  A: 

Hey,

If you convert the string to a date, you can always output it back to the original format using a custom format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Brian
I've tried a few of the example from the link but none have formatted it correctly.
iamjonesy
A: 

Make sure you specify an exact parse format like so:

Console.WriteLine(DateTime.ParseExact("17/12/2010", "dd/mm/yyyy", null));

I am not sure what the last parameter is but it is safe to ignore it.

smaclell
thanks but it didnt work :(
iamjonesy
+4  A: 

Brilliant! thats what I want

That's not what you want. It is the worst possible format for a date because it is so horribly ambiguous. Date string formats depend on the current culture. "4/1/2010" is Unicorn day at SO, it is day in January in Europe. "#4/1/2010#" is a legacy VB6 format.

Always store dates in a DateTime in your code. Always store dates in a database column type of datetime in your dbase. There is never any ambiguity and you'll have an easy time with the DateTime members to manipulate dates.

Hans Passant
thanks i'm going to change my column types.
iamjonesy
A: 

I'm guessing that you are seeing the #m/dd/yyyy# in the debugger, like this screenshot below. Don't worry!

A Date variable isn't stored as a string. The debugger has to convert your Date into a string to display it, and it insists on showing dates in #m/dd/yyyy# format. But that doesn't have any effect on the runtime behaviour of your program.

Screenshot of Visual Studio Debugger

MarkJ
+1  A: 

The correct solution here (at least until you tell us why this isn't possible) is to update your database to use a datetime column type rather than a varchar. Now we also know that this column has no NULL values, because otherwise you'd be complaining about exceptions on your Date.Parse() call. After applying both those sentences, you can trim all that code down to a simple one-liner:

Dim day As Integer = DirectCast(dt.Rows(i)("Date"), DateTime).DayOfWeek

May I also ask why you're looping through the table row by row? I've worked in a shop where that was the norm, but since I've left there I've run in to alternatives and more and more I'm coming to find looping through a datatable as just wrong. It's an older imperative coding style, and generally you want to go for a declarative coding style.

Joel Coehoorn