.net - format date in c# -


How do I specify date as dd / mm / yyyy or mm / dd Can I format? / Yy ?

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

How can I do this in C #?

It is almost identical, just using the dateTime.ToString () method Such as:

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

or:

  datetime DT = gate data (); // GetDate () gives some date dt.ToString ("dd / MM / yy");  

In addition, you may want to consider using one of the predefined date / time formats, for example:

  DateTime.Now .ToString ("g"); // Returns "02/01/2009 9:07 pm" for N / US / or D-CH  

for "01.02.2009 21:07" Make sure that Format is independent of the current local setting, correct

< Some additional, related information:

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

  datetime DT = GetDate (); D.T. ToString ("G", New Culture Info ("N-US")); // Return "5/26/2009 10:39 pm" DT ToString ("G", New Culture Info ("D-CH")); // Return "26.05.2009 22:39"  

Or alternatively, you can set CultureInfo of the current thread before formatting the date:

Thread Present. culture. Agricultural culture = new ("N-US"); Dt.ToString ("live"); // Returns "5/26/2009 10:39 pm" Thread Current Threaded Current Agriculture = New CultureInfo ("D-CH"); Dt.ToString ("live"); // Returns "26.05.2009 22:39"

Comments