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

q3qa4bjr  于 2022-11-22  发布在  jQuery
关注(0)|答案(5)|浏览(202)

我 看到 this 小 提琴 验证 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'});

中 的 每 一 个
请 一定 帮 我 把 这 件 事 做完 。
谢谢

6ju8rftf

6ju8rftf1#

您指定的日期格式是ISO 8601。大多数现代浏览器都支持Dateparsing这种字符串格式。因此,您可以这样做。
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

mbjcgjjk

mbjcgjjk2#

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

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

v440hwme

v440hwme3#

将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

0tdrvxhp

0tdrvxhp4#

对于24小时变化,请用途:

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

isr3a4wc5#

这是我写的,作品很好:

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

请,让我知道,如果你发现一个坏的日期:)

相关问题