c# - 如何在 C# 中将一种日期格式更改为另一种日期格式,例如 10/10/2014 更改为 2014-10-10(年-月-日)?

标签 c# html asp.net asp.net-mvc date

如何在 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/

相关文章:

html - 当其中有文本时,div 会折叠

c# - 如何安装欧文

html - 用最大高度填充底部

c# - Visual C# (Visual Studio) 和 Mono C# GUI 之间的区别

c# - 测试泛型方法参数是否为类

c# - Verifone Vx670 POS 可以用 C# 编程吗?

html - Bootstrap 列在移动设备上重叠文本

mysql - ASP.NET MVC 中是否有一种简单的方法可以对外部 MySQL 服务器执行 INSERT 查询?

c# - 使用 cshtml 和 javascript 动态更改下拉列表

c# - Microsoft 表单预览 API