开发者

c# datetimepicker : how do i get the date in the format? 1/1/2001

i am using winforms, c#, visual studio 2008

the format on the datetimepicker is set to short which means that it will just display the date like this 1/1/2001

h开发者_开发百科owever, i am unable to just get that and NOT the time,

for example this:

MessageBox.Show(dateTimePicker1.Value.Date.ToString());

gives me this:

11/4/2010 12:00:00AM

how do i get just 11/4/2010?


You can use the format in ToString() function. eg.-

dateTimePicker1.Value.Date.ToString("dd/MM/yyyy") ==> gives value "04/11/2010"
or
dateTimePicker1.Value.Date.ToString("dd/MMM/yyyy") ==> gives value "04/Nov/2010"

however you may also use ToShortDateString() function it will return the date-format set in your computer system.... you may also change that from ControlPanel--> Region and Languages --> Date,Time format to "dd/MM/yyyy" to get "04/11/2010" etc.


For time call ToShortTimeString():

dateTimePicker1.Value.ToShortTimeString()

For date call ToShortDateString():

dateTimePicker1.Value.ToShortDateString()

Example of both:

Console.WriteLine(DateTime.Now.ToShortDateString());
Console.WriteLine(DateTime.Now.ToShortTimeString());


dateTimePicker1.Value.Date.ToString("M/d/yyyy")

.ToShortDateString() works too and will take the date formatting from your region.


It should be possible to enter a format in the ToString method, like so:

Date.ToString("d-M-yyyy");


You can do like this way:

dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Short;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜