Connecting Tech Pros Worldwide Help | Site Map

Incorrect date time information displayed when calling Date.toString

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#1: Oct 6 '09
Want to see something weird?
The following code:
Expand|Select|Wrap|Line Numbers
  1. Dim str As New StringBuilder
  2. Dim x As String = "11/6/2009 14:00"
  3. Dim dx As Date
  4. Date.TryParse(x, dx)
  5. str.Append("dx:")
  6. str.Append(Environment.NewLine)
  7. str.Append(dx.Date.ToString)
  8. str.Append(Environment.NewLine)
  9. str.Append(dx.ToString("M/d/yyyy h:mm tt"))
  10. str.Append(Environment.NewLine)
  11. str.Append(dx.Hour.ToString.PadLeft(2, "0"))
  12. str.Append(":")
  13. str.Append(dx.Minute.ToString.PadLeft(2, "0"))
  14. str.Append(":")
  15. str.Append(dx.Second.ToString.PadLeft(2, "0"))
  16. str.Append(Environment.NewLine)
  17. str.Append(Environment.NewLine)
  18.  
  19. MessageBox.Show(str.ToString)
  20.  
Displays:
11/6/2009 12:00 AM
11/6/2009 2:00 PM
14:00:00
What's weird about this is that the Date toString method is incorrect unless provided some sort of format for the time.................

-Frinny
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Oct 7 '09

re: Incorrect date time information displayed when calling Date.toString


That all looks correct to me.
the .Date portion is JUST the date, time information is stripped to "zero" (12:00am), but its STILL a DateTime object.
Oh Frinny, check msdn :-P
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#3: Oct 7 '09

re: Incorrect date time information displayed when calling Date.toString


Yeah I wasn't checking MSDN I was just fooling around.
The weird part is that if you don't use DateTime and you just use Date it still behaves the same way....

Checking MSDN

Ok that is just silly.

The DateTime class has a "Date" property that returns a new DateTime object with only the date set (but the time is left as default which is midnight).

I know that there is a Date in .NET, I've used it before but it seems that the MSDN only has information on Java's Date class (I was using it yesterday when I was playing around...and for some reason the Date Objects also had Time...hmm I remember something about this from school...but it was a long time ago and I obviously haven't been using Date/DateTime Objects since then so it's all a little fuzzy) .

The weird behaviour of the Date/DateTime Object has been explained now thanks to MSDN.

It's working as expected...

Expand|Select|Wrap|Line Numbers
  1.         Dim str As New StringBuilder
  2.         Dim x As String = "11/6/2009 14:00"
  3.         Dim dx As Date
  4.         Date.TryParse(x, dx)
  5.         str.Append("dx:")
  6.         str.Append(Environment.NewLine)
  7.         str.Append(dx.Date.ToString) ' <-- returns a new DateTime Object with just the date set
  8.         str.Append(Environment.NewLine)
  9.         str.Append(dx.ToString) ' <--uses the current DateTime Object 
  10.         str.Append(Environment.NewLine)
  11.         str.Append(dx.ToString("M/d/yyyy h:mm tt"))
  12.         str.Append(Environment.NewLine)
  13.         str.Append(dx.Hour.ToString.PadLeft(2, "0"))
  14.         str.Append(":")
  15.         str.Append(dx.Minute.ToString.PadLeft(2, "0"))
  16.         str.Append(":")
  17.         str.Append(dx.Second.ToString.PadLeft(2, "0"))
  18.         str.Append(Environment.NewLine)
  19.         str.Append(Environment.NewLine)
-Frinny <-- feeling like a newbie all over again
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Oct 7 '09

re: Incorrect date time information displayed when calling Date.toString


Well "Date" must be a VBNET only object, since it's not part of regular .NET. Don't believe it ever has been either. You're regressing back to VB hehe.

You can do DateTime.Now.ToShortDateString(), if you don't want the time info.
Reply


Similar .NET Framework bytes