473,545 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ 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 2091
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.To ShortDateString (), 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
3739
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 bound.
4
2767
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, everything works fine when testing in my computer. However, when i uploaded the web site to my hosting space in the date/time appears as
7
31802
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 current date. If not, I'd like to display the error message to ValidationSummary. It seems to make sense to me to use CompareValidator but the...
12
1730
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 2:56:11.987'). This works fine...if you check the value in the table with query analyzer, it shows in there properly. Now, in my app, I'm executing the...
15
18868
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 the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value...
1
1613
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 windows service, so the application is run every 30min through a scheduled task. When the email is received the received time is ok, but the sent...
2
6701
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 "Activity" in a listview the date fields is displayed like : dd.mm.yy 00:00:00. How can i format this date. I do not want the time. Just the date. ...
2
5073
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, that is GMT +01:00 + daylight saving. And the web service handles datetimes correctly provided that they are also in CEST. But when some client...
6
2222
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 be echoed like a table into html. Almost everything goes well except for one entry going for an hour longer and one disappearing if shorter than...
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.