Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is time coming as 0000 when i am using the todays date in C#

I am trying to create a string to show the current date and time without wanting to use any slashes just the numbers.

For example, 09 jun 2011 11AM should be 201109061100

But when i run the below code the time is always 0000

Output:

ResultLog201109060000

Code:

DateTime currDate = DateTime.Today;
String resultlogFilename;
resultlogFilename = 
    "ResultLog" + 
    currDate.ToString("yyyy") + 
    currDate.ToString("dd") + 
    currDate.ToString("MM") + 
    currDate.ToString("HH") + 
    currDate.ToString("mm");

Any idea how to get the correct time?

like image 566
Prady Avatar asked Dec 30 '25 02:12

Prady


1 Answers

DateTime.Today seems to return the Date part not the hour part.

Just use DateTime.Now to get a complete time.

http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx

Msdn DateTime.Today

Because it returns the current date without the current time, the Today property is suitable for use in applications that work with dates only. For details, see Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo. In contrast, the TimeOfDay property returns the current time without the current date, and the Now property returns both the current date and the current time.

like image 142
CodingBarfield Avatar answered Dec 31 '25 14:12

CodingBarfield