Charles Morrison
Charles Morrison

Reputation: 428

date month not displaying correctly

why is

 string date = string.Format("{0:mmddyyHHmmss}",
    DateTime.Now);

giving me 420813104204

shouldn't it be 040813... ?

I am trying to match mmddyyHHmmSS

Upvotes: 1

Views: 108

Answers (4)

Adil
Adil

Reputation: 148110

You need to use MM for month not mm, mm is for minutes not months.

 string date = string.Format("{0:MMddyyHHmmss}",

You can find more about formats here.

Upvotes: 4

MethodMan
MethodMan

Reputation: 18843

try this instead

string date = string.Format("{0:MMddyyHHmmss}", DateTime.Now);

Upvotes: 3

karthikr
karthikr

Reputation: 99620

string date = string.Format("{0:MMddyyHHmmss}", DateTime.Now);

would give you the required format.

Check out this link for reference

Upvotes: 1

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

That's because mm is for the minute not months, you need to use MM. You can see the definition for custom date time strings here.

string date = string.Format("{0:MMddyyHHmmss}",

Further, I generally don't use string.Format with DateTime objects because I've seen some anomalies when it comes to parsing them across different cultures. Leveraging the ToString method on the DateTime object for me has always been more reliable. That's just something I've seen - it also could be that I was doing something wrong with the string.Format. I wish I could build an example of that right now but I don't even remember what those anomalies are now - I just remember having problems so I switched.

Upvotes: 0

Related Questions