简体   繁体   English

字符串日期格式,C#ASP.NET MVC4

[英]string Date Format, C# ASP.NET MVC4

I'm completely new to C# Asp.Net, and having trouble formatting the following string: 我对C#Asp.Net完全陌生,无法格式化以下字符串:

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

It prints as: YYYYMMDD 它打印为: YYYYMMDD

I would like it to print as: YYYY/MM/DD <- Notice the forward slashes. 我希望将其打印为: YYYY/MM/DD <-注意正斜杠。

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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