如何验证 yyyy-mm-dd hh:mm:ss 格式

4
我看到 this 上的fiddle可以用于验证“ mm/dd/yyyy 或 mm-dd-yyyy ”格式,但我想要验证“ yyyy-mm-dd hh:mm:ss ”格式,同时如何确保今天的日期小于“ yyyy-mm-dd hh:mm:ss ”格式的起始日期?。
这是我的日期时间选择器初始化方式...
$("#startDate, #endDate").datetimepicker({ dateFormat: 'yyyy-mm-dd hh:mm:ss'}); 

请帮我完成这个任务。
谢谢。
5个回答

5

检查yyyy-mm-dd hh:mm:ss字符串是否相互比较非常容易,因为它们已经按顺序排列;您可以忘记数字的基数,并像字符串比较一样简单地执行<>。这可能不适用于其他日期。

function compISOZDate(d1, d2) { // d1 is
    if (d1  <  d2) return -1;   //    smaller
    if (d1 === d2) return  0;   //    the same
    /*   else   */ return  1;   //    bigger
}

验证日期有些复杂,因为月份的天数可能会变化。您可以忽略此事实并仅测试数字,但我更喜欢半途而废,引入上限。

function verifyMyDate(d) {
    var re = /^\d{4}-(0[1-9]|1[0-2])-([0-2]\d|3[01]) (0\d|1[01]):[0-5]\d:[0-5]\d$/;
    //         yyyy -       MM      -       dd           hh     :   mm  :   ss
    return re.test(d);
}

所以例如使用它。
var d1 = '2013-10-07 11:58:26',
    d2 = '2012-06-14 19:22:03';
// check
if (!verifyMyDate(d1)) throw new Error('Invalid date: ' + d1);
if (!verifyMyDate(d2)) throw new Error('Invalid date: ' + d2);
// compare
compISOZDate(d1, d2); //  1, d1 is bigger than d2
// also
compISOZDate(d2, d1); // -1
compISOZDate(d1, d1); //  0

现在,您只需要从输入框中获取值即可。

5
您指定的日期格式为ISO 8601。大多数现代浏览器支持对此字符串格式进行解析,使用Date对象。因此,您可以执行类似以下的操作。

Javascript

var iso8601 = "2013-02-01 10:00:00",
    userDate = new Date(iso8601),
    today = new Date(),
    dateTime,
    date,
    time,
    value;

// check is valid date
if (isNaN(userDate)) {
    alert("invalid userDate");
}

// check if userDate is before today
if (userDate.getDate() < today.getDate()) {
    alert("userDate is in past");
}

// check the string specifically matches "yyyy-mm-dd hh:mm:ss" and is valid
function isGregorianLeapYear(year) {
    return year % 400 === 0 || year % 100 !== 0 && year % 4 === 0;
}

function daysInGregorianMonth(year, month) {
    var days;

    if (month == 2) {
        days = 28;
        if (isGregorianLeapYear(year)) {
            days += 1;
        }
    } else {
        days = 31 - ((month - 1) % 7 % 2);
    }

    return days;
}

if (typeof iso8601 !== "string") {
    alert("not an iso8601 string");
} else {
    dateTime = iso8601.split(" ");
    if (dateTime.length !== 2) {
        alert("missing date or time element");
    } else {
        date = dateTime[0].split("-");
        if (date.length !== 3) {
            alert("incorrect number of date elements");
        } else {
            value = +date[0];
            if (date[0].length !== 4 || value < -9999 || value > 9999) {
                alert("year value is incorrect");
            }

            value = +date[1];
            if (date[1].length !== 2 || value < 1 || value > 12) {
                alert("month value is incorrect");
            }

            value = +date[2];
            if (date[2].length !== 2 || value < 1 || value > daysInGregorianMonth(+date[0], +date[1])) {
                alert("day value is incorrect");
            }
        }

        time = dateTime[1].split(":");
        if (time.length !== 3) {
            alert("incorrect number of time elements");
        } else {
            value = +time[0];
            if (time[0].length !== 2 || value < 0 || value > 23) {
                alert("hour value is incorrect");
            }

            value = +time[1];
            if (time[1].length !== 2 || value < 0 || value > 59) {
                alert("minute value is incorrect");
            }

            value = +time[2];
            if (time[2].length !== 2 || value < 0 || value > 59) {
                alert("second value is incorrect");
            }
        }
    }
}
console.log(userDate);
console.log(today);

jsFiddle


虽然我同意使用_Date_是很好的,但如果OP特别寻找所指示的格式,那么如果_Date_仍然接受它,这将不会显示alert - Paul S.

1

将ValidateDate函数内的RegExp更改为以下代码

function ValidateDate(dtValue)
{
   var dtRegex = new RegExp(/\b\d{4}[\/-]\d{1,2}[\/-]\b\d{1,2} (0\d|1[01]):[0-5]\d:[0-5]\d$\b/);
   return dtRegex.test(dtValue);
}

尝试这个并让我知道,同样的方法你也可以验证hh:mm:ss


0

对于24小时制,使用而不是AM/PM:

regex = new RegExp(^\d{4}-(0[1-9]|1[0-2])-([0-2]\d|3[01]) (0\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$);

0

这是我写的代码,运行良好:

function validateDate(dtValue)   {
  // Format expected dd/mm/aaaa or dd-mm-aaaa (French Date)
  // Accept also d/m/aaaa or d-m-aaaa (ok for MySQL Database to have one number only for days and months)
  // Before the insert into database I will convert to aaaa-mm-jj or aaaa-m-j
  var dtRegex = new RegExp(/\b(0?[1-9]|([1-2]?[0-9]|3[0-1]))[\/-]([0]?[1-9]|1[0-2])[\/-]\b\d{4} ([0-1]?[0-9]|2[0-3]):[0-5]\d$\b/);
  // Accept range => (01 to 31)  (/ or -) (01 to 12) (/ or -) (4 numbers) space (00 to 23) : (00 to 59)

  var bValidateDate= dtRegex.test(dtValue);

  // Test if bValidateDate true, test and throw out (accepted from regex expression) : 
  // 30/02, 31/02, 31/04, 31/06, 31/09, 31/11
  // 30-02, 31-02, 31-04, 31-06, 31-09, 31-11

  // Get the 5 first characters
  var sFebruary29= dtValue.substring(0, 5);

  if (bValidateDate)
  {
    var aOfDateErrors= ["30/02", "31/02", "31/04", "31/06", "31/09", "31/11", "30-02", "31-02", "31-04", "31-06", "31-09", "31-11"];
    if (aOfDateErrors.indexOf(sFebruary29) > -1)
    {
      bValidateDate= false;
    }
  }

  // Then, if bValidateDate is still true (good format)
  // check if the date is a leap year to accept 29/02 or 29-02
  // And finally, my customer asked me to have a year between 2017 and now
  if (bValidateDate)
  {
    // Get the year
    var sYear= dtValue.substring(6, 10);
    // Customer's constraints
    var bYearCustomerOK= ((parseInt(sYear) >= 2017) && (parseInt(sYear) <= new Date().getFullYear()));
    // I promise, this is the "last test'em all !"
    var bFinalDate= (bYearCustomerOK) && (sYear % 400 === 0 || sYear % 100 !== 0 && sYear % 4 === 0) && ((sFebruary29 == "29/02") ||(sFebruary29 == "29-02"));
    if (! bFinalDate)
    {
      bValidateDate= false;
    }
  }

  return bValidateDate;
}

请您知道如果发现了错误的日期,请告诉我:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接