DatePicker日期格式问题与自定义格式

时间:2016-10-31 14:27:40

标签: c# jquery jquery-ui-datepicker

我的电脑有自定义日期格式

PC1 = 31/Oct/2016
PC1 - IIS installed on this pc(PC1)

虽然JQuery DatePicker仅显示10/31/2016并尝试将其保存到C#DataBase中,但它会抛出错误

  

日期格式无效。

我不想硬编码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();

先谢谢 和问候

1 个答案:

答案 0 :(得分:0)

只需使用Datetime.Parse()

dob.DOB = DateTime.Parse(user.testDate, CultureInfo.InvariantCulture);