Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime: how to display as DD.MM.YYYY?

Tags:

c#

.net

I've got DateTime variable and i want to convert it to string "DD.MM.YYYY" Please note, the values must be separated by "dot" sign.

Of course I can do manual string composition. But I wonder if I can use DateTime.ToString() to do required conversion.

like image 301
Captain Comic Avatar asked Sep 27 '10 13:09

Captain Comic


People also ask

How do I convert DateTime to date format?

You can convert a DATETIME to a DATE using the CONVERT function. The syntax for this is CONVERT (datetime, format). This shows the date only and no time.

How do I print a date in dd-mm-yyyy format in Python?

Use strftime() function of a datetime class The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I change the format of a DateTime object in Python?

Function used. strftime() can change the date format in python.


2 Answers

Yes, you can:

string formatted = dt.ToString("dd'.'MM'.'yyyy");

Now in this case the quotes aren't actually required, as custom date/time format strings don't interpret dot in any special way. However, I like to make it explicit - if change '.' for ':' for example, then while it's quoted it will stay with the explicit character, but unquoted it would be "the culture-specific time separator". It wasn't entirely obvious to me whether "." would be interpreted as "the culture-specific decimal separator" or not, hence the quoting. You may feel that's over the top, of course - it's entirely your decision.

You may also want to specify the invariant culture, just to remove any other traces of doubt:

string formatted = dt.ToString("dd'.'MM'.'yyyy", CultureInfo.InvariantCulture);

(At that point the quotes around the dot become less relevant, as "." is the decimal separator in the invariant culture anyway.)

like image 125
Jon Skeet Avatar answered Sep 22 '22 00:09

Jon Skeet


Yes, you can use DateTime.ToString like this:

myDateVariable.ToString("dd.MM.yyyy");

Note that you have to use capital MM here, since mm evaluates to minutes instead of months.

like image 33
Øyvind Bråthen Avatar answered Sep 22 '22 00:09

Øyvind Bråthen