自定义格式的 DatePicker 日期格式问题
DatePicker date format issue with custom format
我的电脑有自定义日期格式
PC1 = 31/Oct/2016
PC1 - IIS installed on this pc(PC1)
虽然 JQuery DatePicker
仅显示 10/31/2016
并尝试将其保存到 C# 数据库中,但它抛出错误
Invalid date format.
我不想对 DatePicker
格式进行硬编码,而是希望根据 PC 设置自动继承日期格式。
请告诉我怎么做。
已编辑
$("#txtDBOEdit").datepicker(
{
// dateFormat:'dd/MMM/yyyy' works fine but I dont want to hard code format and adopt format automatically from local machine.
changeMonth: true,
changeYear: true,
maxDate: 0,
yearRange: "-100:+0",
minDate: new Date(year, month, day),
defaultDate: new Date(year + 50, month, day)
});
C#
var dob = _db.Users.Where(w => w.UserNumber == user.UserNumber).FirstOrDefault();
if (dob != null)
{
dob.DOB = Convert.ToDateTime(user.testDate);
// user.testDate='10/31/2016' which is comming for datepicket which is default date format.
//dob.DOB = Convert.ToDateTime("04/JAN/2016"); it works fine because it is according to local pc custom date foramt
}
_db.SaveChanges();
提前致谢
和问候
只需使用 Datetime.Parse()
dob.DOB = DateTime.Parse(user.testDate, CultureInfo.InvariantCulture);
我的电脑有自定义日期格式
PC1 = 31/Oct/2016
PC1 - IIS installed on this pc(PC1)
虽然 JQuery DatePicker
仅显示 10/31/2016
并尝试将其保存到 C# 数据库中,但它抛出错误
Invalid date format.
我不想对 DatePicker
格式进行硬编码,而是希望根据 PC 设置自动继承日期格式。
请告诉我怎么做。
已编辑
$("#txtDBOEdit").datepicker(
{
// dateFormat:'dd/MMM/yyyy' works fine but I dont want to hard code format and adopt format automatically from local machine.
changeMonth: true,
changeYear: true,
maxDate: 0,
yearRange: "-100:+0",
minDate: new Date(year, month, day),
defaultDate: new Date(year + 50, month, day)
});
C#
var dob = _db.Users.Where(w => w.UserNumber == user.UserNumber).FirstOrDefault();
if (dob != null)
{
dob.DOB = Convert.ToDateTime(user.testDate);
// user.testDate='10/31/2016' which is comming for datepicket which is default date format.
//dob.DOB = Convert.ToDateTime("04/JAN/2016"); it works fine because it is according to local pc custom date foramt
}
_db.SaveChanges();
提前致谢 和问候
只需使用 Datetime.Parse()
dob.DOB = DateTime.Parse(user.testDate, CultureInfo.InvariantCulture);