how to fix 'The field must be a date' on a datetime property in mvc

前端 未结 13 1128
逝去的感伤
逝去的感伤 2020-12-29 06:59

I need to capture date and time both for my model property. In my model class I have the following

[Required]
[DataType(DataType.DateTime)]
public DateTime?          


        
13条回答
  •  攒了一身酷
    2020-12-29 07:26

    localization or moment.js was not work for me while using datetimepicker. splitting hour and then checking is working. I am using format: 'dd.mm.yyyy hh:ii' for datetimepicker.

    $.validator.methods.date = function (value, element) {
        var dateL = value.split(" ");
        var date = dateL[0].split(".");
        var hour = dateL.length > 1 ? dateL[1].split(":") : "";
        return this.optional(element) || !/Invalid|NaN/.test(dateL.length > 1 ? new Date(date[2], date[1], date[0],hour[0],hour[1]).toString() : new Date(date[2], date[1], date[0]).toString());
    }
    

提交回复
热议问题