如何在C#例如10/10/2014到2014-10-10(年 - 月 - 日)中将1种日期格式更改为另一种格式?
用户选择带有日期选择器的日期,然后它出现在文本框中,但格式为10/10/2014(日/月/年)。但我需要将此输出转换为反映此格式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" />
答案 0 :(得分:2)
从jQuery UI datepicker documentation设置日期选择器时,请指定格式:
$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
答案 1 :(得分:2)
对于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);
}