简体   繁体   中英

Date and time formatting

How do I convert a date time value in a C# application into a string that has the form 6-18-2012 13:12:13.0 ? I keep getting leading zeros. In addition, I can't seem to get it into military time.

myDateTime.ToString("M-d-yyyy HH:mm:ss.f")

如果您也不希望小时中的前导零,请执行以下操作:

myDateTime.ToString("M-d-yyyy H:mm:ss.f")

Try this:

date.ToString( "M-d-yyyy H:mm:ss.f" );

That string will not add a leading 0 to the month, day, or hour. More information can be found at Microsoft's Custom Date and Time Format Strings .

You can too use:

string.Format("{0:Md-yyyy H:mm:ss.f}", yourDateTime);

More details in C# Examples

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