如何在 C# 中将一种日期格式更改为另一种,例如 10/10/2014 更改为 2014-10-10(年-月-日)?
用户使用日期选择器选择一个日期,然后它会出现在文本框中,但格式为 10/10/2014(日/月/年)。但我需要转换此输出以反射(reflect)此格式 2014-10-10(年-月-日)
在 asp.net-mvc 中
代码:
@Html.Label("Start", "Start Date:")
@Html.TextBox("Start", string.Empty, new {@id = "Start", @class = "datepicker"})
@Html.Label("endd", "End Date:")
@Html.TextBox("endd", string.Empty, new {@id = "End", @class = "datepicker"})
<input type="submit" value="Apply" id ="DateSelected" />
最佳答案
对于 C# 转换将是:
public static string convert(string dateValue){
string pattern = "MM/dd/yyyy";
DateTime parsedDate;
if (DateTime.TryParseExact(dateValue, pattern, null,
DateTimeStyles.None, out parsedDate)){
return String.Format("{0:yyyy-MM-dd}",parsedDate);
}
return null;
}
//usage example
string dateValue="10/10/2014";
string converted=convert(dateValue);
if(converted!=null){
Console.WriteLine("Converted '{0}' to {1}.",
dateValue, converted);
}
关于c# - 如何在 C# 中将一种日期格式更改为另一种日期格式,例如 10/10/2014 更改为 2014-10-10(年-月-日)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526425/