19

How can I format a date as dd/mm/yyyy or mm/dd/yy ?

Like in VB format("dd/mm/yy",now)

How can I do this in C#?

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Gold
  • 60,526
  • 100
  • 215
  • 315

7 Answers7

46

It's almost the same, simply use the DateTime.ToString() method, e.g:

DateTime.Now.ToString("dd/MM/yy");

Or:

DateTime dt = GetDate(); // GetDate() returns some date
dt.ToString("dd/MM/yy");

In addition, you might want to consider using one of the predefined date/time formats, e.g:

DateTime.Now.ToString("g");
// returns "02/01/2009 9:07 PM" for en-US
// or "01.02.2009 21:07" for de-CH 

These ensure that the format will be correct, independent of the current locale settings.

Check the following MSDN pages for more information


Some additional, related information:

If you want to display a date in a specific locale / culture, then there is an overload of the ToString() method that takes an IFormatProvider:

DateTime dt = GetDate();
dt.ToString("g", new CultureInfo("en-US")); // returns "5/26/2009 10:39 PM"
dt.ToString("g", new CultureInfo("de-CH")); // returns "26.05.2009 22:39"

Or alternatively, you can set the CultureInfo of the current thread prior to formatting a date:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
dt.ToString("g"); // returns "5/26/2009 10:39 PM"

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-CH");
dt.ToString("g"); // returns "26.05.2009 22:39"
M4N
  • 94,805
  • 45
  • 217
  • 260
  • 2
    I encourage you to follow Martin's advice and go with the predefined codes, which are culture aware. You might not need it now but in the future it could make things easier. – Michael Haren Feb 01 '09 at 21:48
9
string.Format("{0:dd/MM/yyyy}", DateTime.Now)

Look up "format strings" on MSDN to see all formatting options.

Use yy, yyyy, M, MM, MMM, MMMM, d, dd, ddd, dddd for the date component

Use h, hh, H, HH, m, mm, s, ss for the time-of-day component

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59
  • Here is the MSDN Link. Might helps others. https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx – Sai May 17 '15 at 04:43
3

Try this :

String.Format("{0:MM/dd/yyyy}", DateTime.Now); // 01/31/2009
String.Format("{0:dd/MM/yyyy}", DateTime.Now); // 31/01/2009
String.Format("{dd/MM/yyyy}", DateTime.Now); // 31/01/2009
S.Lott
  • 384,516
  • 81
  • 508
  • 779
Canavar
  • 47,715
  • 17
  • 91
  • 122
  • This example will in turn call Date.ToString(string format), as shown below in Martin's answer. That is a simpler, more direct way, than going through String.Format. – abelenky Feb 01 '09 at 20:12
  • yes, you are right, Martin's answer is the direct one, thanks. – Canavar Feb 01 '09 at 20:43
3

In you can also write

DateTime aDate = new DateTime(); 
string s = aDate.ToShortDateString();

for a short notation

or

DateTime aDate = new DateTime(); 
string s = aDate.ToLongDateString();

for a long notation like "Sunday, Febuary 1, 2009".

Or take a look at MSDN for the possibities of .ToString("???");

Sorskoot
  • 10,190
  • 6
  • 55
  • 98
3

Better yet, use just

DateTime.Now.ToString() 

or

DateTime.Now.ToString(CultureInfo.CurrentCulture) 

to use the format the user prefers.

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Nir
  • 29,306
  • 10
  • 67
  • 103
1

I ran into the same issue. What I needed to do was add a reference at the top of the class and change the CultureInfo of the thread that is currently executing.

using System.Threading;

string cultureName = "fr-CA";
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);

DateTime theDate = new DateTime(2015, 11, 06);
theDate.ToString("g");
Console.WriteLine(theDate);

All you have to do is change the culture name, for example: "en-US" = United States "fr-FR" = French-speaking France "fr-CA" = French-speaking Canada etc...

user2146538
  • 334
  • 1
  • 5
  • 23
1

I think this is simple as you can convert to and from any format without any confusion

 DateTime.ParseExact(txt.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy/MM/dd"));
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87