简体   繁体   中英

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

however, 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 ?

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()也可以使用你所在地区的日期格式。

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

Date.ToString("dM-yyyy");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM