Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant get hour and minute of the day

Tags:

c#

datetime

I am trying to get the current time:

date = "(" + DateTime.Today.Year.ToString() + "-" + 
DateTime.Today.Month.ToString() + "-" + DateTime.Today.Day.ToString() + " " + 
"(" + DateTime.Today.Hour.ToString() + ":" + DateTime.Today.Minute.ToString() 
+")" + ")";

This should get a date like:

(2013-2-1 (13:01))

But it give me:

(2013-2-1 (0:0))

How can i fix this?

like image 691
Pomster Avatar asked Nov 27 '22 17:11

Pomster


1 Answers

You're using DateTime.Today which is documented as:

An object that is set to today's date, with the time component set to 00:00:00.

So yes, if you find the Hour and Minute components, they will be 0...

If you want the current time of day, use DateTime.Now instead. Note that both Today and Now use the system-local time zone - you need to be sure that that really is what you want to use. (It's probably fine for a local client app, but not for a web app.)

like image 106
Jon Skeet Avatar answered Dec 18 '22 01:12

Jon Skeet