i am trying to understand how DateTime.ToString(Date pattern) work in .net framework, C#.
I changed my computer to have a short Date format like this yyyy.MM.dd. Following is what I notice
DateTime myDate = DateTime.Now;
myDate.ToString("yyyy/MM/dd")
always return in the format of yyyy.MM.dd not yyyy/MM/dd
and
myDate.ToString("yyyy-MM-dd")
does return string in the format of yyyy-MM-dd
to have it return what i was looking for, this is what i need to do myDate.ToString("yyyy'/'MM'/'dd")
===> yyyy/MM/dd
Can anyone explain to me why it is doing that? and is there any other way i can achieve the same result?
thanks....
/ is considered a format specifier and is replaced just like yyyy is.
Read the information on the format specifiers :
/ = The default date separator defined in DateSeparator.
You're getting the behavior you're seeing because "/" is a format specifier .
If you look at the custom date format settings help, you'll see that "/" translates as the date separator for your culture.
You can also escape single character using backslash:
DateTime myDate = DateTime.Now;
myDate.ToString("yyyy\\/MM\\/dd");
// or
myDate.ToString(@"yyyy\/MM\/dd");
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.