开发者

Yet another date formatting problem :(

I seem to have 开发者_JS百科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


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.


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


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.


Are you parsing it like this:

Dim newDate as DateTime = DateTime.Parse(myDate)


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)


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.


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 http://img707.imageshack.us/img707/6205/debugger.gif

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜