如何验证yyyy-mm-dd hh:mm:ss格式 [英] How do I validate yyyy-mm-dd hh:mm:ss format

查看:124
本文介绍了如何验证yyyy-mm-dd hh:mm:ss格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个小提琴,用于验证 mm / dd / yyyy或mm -dd-yyyy 但我想验证 yyyy-mm-dd hh:mm:ss 格式我如何确保今天更小比起来自 yyyy-mm-dd hh:mm:ss 格式的日期?。

I saw this fiddle for validating mm/dd/yyyy or mm-dd-yyyy but I would like to validate yyyy-mm-dd hh:mm:ss format also how do I ensure that today is lesser than from date with the yyyy-mm-dd hh:mm:ss format?.

这就是我已启动我的日期时间选择器..

this is how I have initiated my date time picker..

$("#startDate, #endDate").datetimepicker({ dateFormat: 'yyyy-mm-dd hh:mm:ss'}); 

请帮我完成这项工作。

谢谢

推荐答案

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

The date format that you have specified is ISO 8601. Most modern browsers support Date parsing of this string format. So you can do something like this.

Javascript

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

这篇关于如何验证yyyy-mm-dd hh:mm:ss格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆