Hari Gillala
Hari Gillala

Reputation: 11916

C# format of date issue

My problem: I need to get date format as "mm/dd/yyyy"

Scenario:

I have declared DateBirth as nullable DateTime.

The value I get when I use:

AdvancedObj.DateBirth .Value.ToString()

is: "13/03/2013 00:00:00"

The value I get when I use

AdvancedObj.DateBirth .Value.ToString(CultureInfo.InvariantCulture)

is :"03/13/2013 00:00:00"//This is roughly correct but, I do not need 00:00:00

I have tried this as well, but the format is correct and value is incorrect.

AdvancedObj.DateBirth.Value.ToString("dd/mm/yyyy",CultureInfo.GetCultureInfo("en-Us"))


 **"13/00/2013"**

Can anybody point me, what am I missing?

Upvotes: 0

Views: 68

Answers (2)

Cyril Gandon
Cyril Gandon

Reputation: 17048

Month are 'M'. 'm' is for minutes.

"dd/MM/yyyy"

Upvotes: 1

Oded
Oded

Reputation: 498904

Use the right format string for months - it is MM. mm is for minutes:

AdvancedObj.DateBirth.Value.ToString("MM/dd/yyyy",CultureInfo.InvariantCulture)

Also, order them correctly as above - if you want months before days, use MM/dd/yyyy, if the other way around, dd/MM/yyyy.

I suggest you take a good long read of Custom Date and Time Format Strings on MSDN.

Upvotes: 2

Related Questions