简体   繁体   中英

DateTimePicker Date formatting value

I am getting date value correctly from DateTimePicker using DateTimePicker.Value .

However I wish to format my date to be YYYYMMDD ( ex: "20151020" ). Is this possible please?

使用以下代码可以实现简单的格式化。

DateTimePicker.Value.ToString("yyyyMMdd");

DateTimePicker.Value returns DateTime , not a string . A DateTime does not have any implicit format. It just have date and time values. It's textual representation can have a format which is usually done with ToString() method like;

string myFormat = DateTimePicker.Value.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

Remember, there is no YYYY and DD as a custom date and time format strings . They should be as yyyy and dd since they are case sensitive.

Seems you want to have your result in string format:

string dateFormat = "yyyyMMdd";
string result = DateTimePicker.Value.ToString(dateFormat);

For other formats, here's the reference:

http://www.csharp-examples.net/string-format-datetime/

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