开发者

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

I am trying to create a strin开发者_高级运维g 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?


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.


var resultlogFilename = string.Format("ResultLog{0:yyyyddMMhhmm}", DateTime.Now);


Use DateTime.Now instead of .Today. "Today" filters out the "time" part, so it effectively returns midnight (0:00).


It is because today returns to you only todays date and not todays time :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜