如何在jQuery中设置日期模式datepicker

问题描述:

我已经根据客户区域转换日期时间格式。 我一直在使用C#如何在jQuery中设置日期模式datepicker

public CultureInfo GetDateFormat() 
    { 
     var _CultureInfo = new CultureInfo(1043); 
     return _CultureInfo; 
    } 

    public string GetFormattedDateString(string _DateToFormat) 
    { 
     string _tempDate = string.Empty; 

     if (!string.IsNullOrEmpty(_DateToFormat)) 
     { 
      _tempDate = DateTime.Parse(_DateToFormat).ToString(GetDateFormat().DateTimeFormat.ShortDatePattern); 
     } 
     return _tempDate; 
    } 

这正常工作与下面的代码做了。但如果我在jQuery datepicker中使用相同的方法,它会给出不同的格式。

例:在c#

2015年10月10日

jquery

2015年10月10日

下面是jQuery的代码格式更改,

public string GetDateFormat() 
{ 
    var pattern = new CultureInfo(1043).DateTimeFormat.ShortDatePattern; 

    return pattern; 
} 

@Html.DatePicker(new { 
        name = "filter.StartDate", 
        value = Model.Filter.StartDate, 
        data_bind = "value: StartDate", 
        data_datepicker_options = "{dateFormat: '" + GetDateFormat() + "'}" 
}) 

上面是创建日期选择器的剃须刀代码。

有没有办法在c#和datepicker输出中获取相同的日期格式?

+0

你有没有试过明确的'dateFormat:'dd-mm-yy''? –

在你的日期选择器,你可以添加此

dateFormat: 'dd-MM-yyyy' 

更改格式

或在你的模型,你可以添加这个在它

顶部

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")] 

- 另请参阅此post。它对于剃刀中的dateformats非常有用。

+0

其实我不能硬编码你提到的格式,因为格式可能会根据用户的文化而改变。我在记录过程中获取用户文化并相应地更改日期格式。 – user3649433