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

How do I validate yyyy-mm-dd hh:mm:ss format

本文关键字:hh mm ss 格式 yyyy-mm-dd 何验证 验证      更新时间:2023-09-26

>我看到了这个用于验证mm/dd/yyyy or 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'}); 

请帮我完成这项工作。

谢谢

您指定的日期格式为 ISO 8601。大多数现代浏览器都支持Date解析这种字符串格式。所以你可以做这样的事情。

爪哇语

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);

js小提琴

检查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

现在剩下的就是从输入中获取值。

将 ValidateDate 函数中的正则表达式更改为以下代码

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 一样

对于 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$);
这是我

写的,工作正常:

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;
}

如果您发现约会不好,请告诉我:)