4

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?

andy
  • 5,979
  • 2
  • 27
  • 49
Pomster
  • 14,567
  • 55
  • 128
  • 204

9 Answers9

9

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Instead of all the concatenation you are using at the moment, use the DateTime object's ToString() method...

string date = DateTime.Now.ToString("(yyyy-M-d (hh:mm))");

Also, as pointed out by many others, note the use of DateTime.Now instead of DateTime.Today

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
2

Use DateTime.Now instead of DateTime.Today

The DateTime.Today has its time part set to 00:00:00
The DateTime.Now gets the current date and time for the local computer

Steve
  • 213,761
  • 22
  • 232
  • 286
2

You have DateTime.Today which truncates the time element away.

Use DateTime.Now instead which gives you the full date and time.

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
2

Use DateTime.Now instead of DateTime.Today. DateTime.Today doesn't include the time, only the date. See also: Difference between System.DateTime.Now and System.DateTime.Today

Community
  • 1
  • 1
antonijn
  • 5,702
  • 2
  • 26
  • 33
2

Use NodaTime if its a web application. Use DateTime.Now for local date and time.

So it should be:

date = "(" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + " " + "(" + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() +")" + ")";
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Calling `DateTime.Now` multiple times is never a good thing. And Noda Time is awesome, but it is not limited to web applications. This code doesn't demonstrate Noda Time anyway, so I'm not sure what you were getting at. – Matt Johnson-Pint Jul 24 '13 at 00:33
2

Please try this shorter code for current date and time as per your format

var date = DateTime.Now.ToString("(yyyy-MM-dd(HH:mm))");

Cris
  • 12,799
  • 5
  • 35
  • 50
2

Use the following code instead of concatenation. Its much more optimized then the concatenation work you have done.

string date = DateTime.Now.ToString("(yyyy-M-d (hh:mm))");

Also DateTime.Today does not have time element in it.

Tom
  • 26,212
  • 21
  • 100
  • 111
1

DateTime.Today will return u 2013-2-1 00:00:00 .. It gives the time sure but the timestamp is set to 00:00:00.. that is the reason why u r not getting an exception n still getting 00:00:00. Use DateTime.Now for current timestamp.

Raghavan
  • 637
  • 3
  • 12