如何将C#中的1个日期格式更改为另一个,例如2014年10月10日至2014年10月10日(年-月-日)?
如何将C#中的1个日期格式更改为另一个,例如2014年10月10日至2014年10月10日(年-月-日) 用户使用日期选择器选择日期,然后该日期显示在文本框中,但格式为10/10/2014(日/月/年)。但我需要将此输出转换为反映2014-10-10(年-月-日)格式 在asp.net-mvc中 代码:如何将C#中的1个日期格式更改为另一个,例如2014年10月10日至2014年10月10日(年-月-日)?,c#,html,asp.net,asp.net-mvc,date,C#,Html,Asp.net,Asp.net Mvc,Date,如何将C#中的1个日期格式更改为另一个,例如2014年10月10日至2014年10月10日(年-月-日) 用户使用日期选择器选择日期,然后该日期显示在文本框中,但格式为10/10/2014(日/月/年)。但我需要将此输出转换为反映2014-10-10(年-月-日)格式 在asp.net-mvc中 代码: @Html.Label(“开始”,“开始日期:”) @TextBox(“Start”,string.Empty,new{@id=“Start”,@class=“datepicker”}) @标签
@Html.Label(“开始”,“开始日期:”)
@TextBox(“Start”,string.Empty,new{@id=“Start”,@class=“datepicker”})
@标签(“endd”,“结束日期:”)
@TextBox(“endd”,string.Empty,new{@id=“End”,@class=“datepicker”})
在设置日期选择器时,从中指定格式:
$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
对于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);
}
请看一下这里这看起来像
asp.NETMVC
。您是否尝试过格式化DateTime
值?它在asp.net-mvc中。您使用的是哪个日期选择器库?jqueryui中的一个
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);
}