I have converted the datetime format based on client region. I have done it with below code using c#
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;
}
this works fine. But if I use the same approach in jQuery datepicker it gives a different format.
Ex: In c#
10-10-2015
in jquery
10/10/2015
Below is the code for jQuery format change,
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() + "'}"
})
above is the razor code which creates the date picker.
Is there a way to get same date format in c# and datepicker output?
In your datepicker you could add this
dateFormat: 'dd-MM-yyyy'
to change the format
or in your model you could add this
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
on top of it
-See also this post . Its very useful for dateformats in razor.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.