473,480 Members | 2268 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Incorrect date time information displayed when calling Date.toString

Frinavale
9,735 Recognized Expert Moderator Expert
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
Oct 6 '09 #1
3 2086
Plater
7,872 Recognized Expert Expert
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
Oct 7 '09 #2
Frinavale
9,735 Recognized Expert Moderator Expert
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
Oct 7 '09 #3
Plater
7,872 Recognized Expert Expert
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.
Oct 7 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3735
by: HJ | last post by:
Hi all, Consider an Access 2002 run-time application on Windows XP. The controls on a form are locked until the user clicks an Edit button. The form is based on a query and all controls are...
4
2755
by: Miguel Dias Moura | last post by:
Hello, i am just finishing a web site in ASP.net / VB using Dreamweaver MX 2004 and in all the pages i use the date/time format as follows: Date: DD-MM-YYYY Time: HH-MM (24 hours) Anyway,...
7
31789
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
12
1719
by: Rob T | last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be precise so the value is stored to the 1000th of a second. ie "insert into myTable mydate values ('08/05/2005...
15
18859
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
1
1607
by: bonokoot | last post by:
Hello, I wrote this program in C# that accesses a SQL Server database that runs a stored procedure and sents the results in an email every 30min. I wrote this as a windows application instead of a...
2
6696
by: Lars Erik Nes | last post by:
Hi all. I have a problem. I get all my data from a dataset (not mssql) and in that dataset i have a table called "Activity". In this table i have a few date fields. When i display the dataset...
2
5059
by: nightwatch77 | last post by:
Hi, does anyone know why .Net incorrectly handles time zone conversion when passing DateTime through web services? The problem is that it seems to ignore the time zone part. My time zone is CEST,...
6
2218
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.