简体   繁体   中英

string Date Format, C# ASP.NET MVC4

I'm completely new to C# Asp.Net, and having trouble formatting the following string:

string defTo = string.Format (@"{0:yyyy\/MM\/dd}" , DateTime.Now);

It prints as: YYYYMMDD

I would like it to print as: YYYY/MM/DD <- Notice the forward slashes.

Can someone point out how I could achieve this?

尝试这个:

string defTo =DateTime.Now.ToString("yyyy/MM/dd")

删除反斜杠:

string defTo = string.Format("{0:yyyy/MM/dd}", DateTime.Now);

尝试

DateTime.Now.ToString("yyyy/MM/dd")

You were close:

string defTo = string.Format("{0:dd\\/MM\\/yyyy}", DateTime.Now);

EDIT: This should work too:

"{0:dd'/'MM'/'yyyy}"

您不需要为“ /”使用转义字符,只需将其删除:

string defTo = string.Format ( @"{0:yyyy/MM/dd}" , DateTime.Now );

You don't need the forward slashes since you declared it a literal string @"..." .

Anything inside the quotes are "as-is" except for other quote characters. To escape them you should double them:

var str1 = @"this is a ""double quote"" in a literal string";

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