Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to get the time of system by simple coding of

System.DateTime.Today.ToString("hh:mm tt")
But every time I run this code I get the same output of "12:00 AM"

Why my system time is not fetching here?
Need Help!!
Posted
Updated 29-Mar-11 10:13am
v2
Comments
Dalek Dave 29-Mar-11 16:13pm    
Edited for Readability.

Instead of Today (which is only the date), try using Now:
System.DateTime.Now.ToString...
 
Share this answer
 
Comments
Manfred Rudolf Bihy 29-Mar-11 15:26pm    
Actually Today has the time set to 00:00:00 which is the same as 12:00 AM. Still my 5!
Wendelius 29-Mar-11 15:27pm    
Yes, it's a constant. That's a good clarification.
Sergey Alexandrovich Kryukov 29-Mar-11 15:56pm    
This is good, my 5, even though there is no such thing "only the date" here.
Generally, many people (well, maybe only the weak-minded ones :-) were confused due to this long and misleading tradition of using the term DateTime. Time and calendar is the area where the traditional idiotism of the programming community was manifested in the most bright ways and most regular manner, just remember "the problem of 2000". Naming something "DateTime" is like "DateHourTime" or "YearTime". Time is time, the data is only one part of it.

To reduce my irritation, in a file using this type I tend to put at the beginning of my namespace:

using Time = System.DateTime; // :-)

--SA
Wendelius 29-Mar-11 16:13pm    
Thanks and a good comment. Y2K was a funny thing in overall and now they tell that we're hitting another AbsoluteEndDateTime at the end of 2012 :)
Actually Mika already gave the correct solution, but I wanted to elaborate a bit more. The difference between DateTime.Today and DateTime.Now is that Today give you the start of the current day and this is:

2011-03-29T00:00:00 which is today at 12:00 AM.
Now also has the current time attached: 2011-03-29T21:22:34

That just about sums it up!

Cheers,
 
Share this answer
 
Comments
Wendelius 29-Mar-11 15:28pm    
Good addition, my 5
Sergey Alexandrovich Kryukov 29-Mar-11 15:50pm    
My 5.
--SA
Sergey Alexandrovich Kryukov 29-Mar-11 15:57pm    
Funny stuff: please also see my comment to Mika's answer.
--SA
Dalek Dave 29-Mar-11 16:12pm    
Good Call Manfred
Manfred Rudolf Bihy 29-Mar-11 16:26pm    
Thanks Dave.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900