.net - format date in c# -
how can format date dd/mm/yyyy
or mm/dd/yy
?
like in vb format("dd/mm/yy",now)
how can in c#?
it's same, use datetime.tostring()
method, e.g:
datetime.now.tostring("dd/mm/yy");
or:
datetime dt = getdate(); // getdate() returns date dt.tostring("dd/mm/yy");
in addition, might want consider using 1 of predefined date/time formats, e.g:
datetime.now.tostring("g"); // returns "02/01/2009 9:07 pm" en-us // or "01.02.2009 21:07" de-ch
these ensure format correct, independent of current locale settings.
check following msdn pages more information
some additional, related information:
if want display date in specific locale / culture, there overload of tostring()
method takes 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, can set cultureinfo
of current thread prior formatting 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"