29

I am trying to get todays date in GBR format but,

DateTime.Now.ToString("dd/mm/yyyy");

returns with the value of 08/00/2013

So 00 in the month group was returned instead of returning the month 04.

Any ideas why this happened?

Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121

6 Answers6

54

Lower mm means minutes, so

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

or

DateTime.Now.ToString("d");  

or

DateTime.Now.ToShortDateString()

works.

Standard Date and Time Format Strings

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 2
    Regarding the one who tried to edit my answer, the order the days, month and year appears depends on the current culture. Thats why you have noticed a difference between the first and the other approaches. You can alway use a spefic culture, f.e. by using the overload of DateTime.ToString. – Tim Schmelter Aug 15 '14 at 17:05
  • The only point I'd make with this solution is that these return 2 different formats when you have single digit day or month, DateTime.Now.ToString("dd/MM/yyyy") gives you say 01/01/2017 where as the other two would give you 1/1/2017. – ScottFoster1000 Oct 06 '17 at 17:18
11

use MM(months) instead of mm(minutes) :

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

check here for more format options.

Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
6

It should be MM for months. You are asking for minutes.

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

See Custom Date and Time Format Strings on MSDN for details.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • hi, I am using the above code exactly, when executed from iis automaticaly date seperated using '-' instead of '/'. i have created a log for that to check the input. can you help me to solve this, iis machine date format is dd/MM/yyyy only. – Pranesh Janarthanan Aug 30 '16 at 07:09
  • i set a value to the textbox Textbox.Text = DateTime.Today.ToString("dd/MM/yyyy"); but it recognize as: 30-08-2016 – Pranesh Janarthanan Aug 30 '16 at 07:17
  • i guess i found solution here http://stackoverflow.com/questions/6343289/why-does-datetime-tostringdd-mm-yyyy-give-me-dd-mm-yyyy – Pranesh Janarthanan Aug 30 '16 at 07:22
6

In addition to what the other answers have said, note that the '/' character in "dd/MM/yyyy" is not a literal character: it represents the date separator of the current user's culture. Therefore, if the current culture uses yyyy-MM-dd dates, then when you call toString it will give you a date such as "31-12-2016" (using dashes instead of slashes). To force it to use slashes, you need to escape that character:

DateTime.Now.ToString("dd/MM/yyyy")     --> "19-12-2016" for a Japanese user
DateTime.Now.ToString("dd/MM/yyyy")     --> "19/12/2016" for a UK user
DateTime.Now.ToString("dd\\/MM\\/yyyy") --> "19/12/2016" independent of region
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
3

Use MM for months. mm is for minutes.

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

You probably run this code at the begining an hour like (00:00, 05.00, 18.00) and mm gives minutes (00) to your datetime.

From Custom Date and Time Format Strings

"mm" --> The minute, from 00 through 59.

"MM" --> The month, from 01 through 12.

Here is a DEMO. (Which the month part of first line depends on which time do you run this code ;) )

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

There are only minor error.Use MM instead of mm ,so it will be effective write as below:

 @DateTime.Now.ToString("dd/MM/yyy")
seven
  • 97
  • 1
  • 4