Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Now.ToShortDateString(); replace month and day

I need to change format of

this.TextBox3.Text = DateTime.Now.ToShortDateString();

so it returns (for example) 25.02.2012, but I need 02.25.2012

How can this be done?

like image 580
cnd Avatar asked Feb 02 '10 05:02

cnd


4 Answers

Use DateTime.ToString with the specified format MM.dd.yyyy:

this.TextBox3.Text = DateTime.Now.ToString("MM.dd.yyyy");

Here, MM means the month from 01 to 12, dd means the day from 01 to 31 and yyyy means the year as a four-digit number.

like image 110
jason Avatar answered Oct 11 '22 02:10

jason


Little addition to Jason's answer:

  1. The ToShortDateString() is culture-sensitive.

From MSDN:

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.

That's mean it's better to use the ToString() method and define format explicitly (as Jason said). Although if this string appeas in UI the ToShortDateString() is a good solution because it returns string which is familiar to a user.

  1. If you need just today's date you can use DateTime.Today.
like image 14
bniwredyc Avatar answered Oct 11 '22 02:10

bniwredyc


this.TextBox3.Text = DateTime.Now.ToString("MM.dd.yyyy");
like image 7
Hogan Avatar answered Oct 11 '22 02:10

Hogan


Try this:

this.TextBox3.Text = String.Format("{0: MM.dd.yyyy}",DateTime.Now);
like image 1
mark Avatar answered Oct 11 '22 00:10

mark