Javascript 如何在javascript中验证时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12422918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:56:32  来源:igfitidea点击:

How to validate timestamp in javascript

javascriptvalidationtimestamp

提问by prashant

How do you validate timestamp using javascript and timestamp to accept multiple formats e.g. YYYY-MM-DD HH:mm:ss.S, YYYY-MM-DD HH:mm:ss AM/PM.

您如何使用 javascript 和时间戳验证时间戳以接受多种格式,例如 YYYY-MM-DD HH:mm:ss.S、YYYY-MM-DD HH:mm:ss AM/PM。

回答by J?rgen

You can validate if a string is a valid timestamp like this:

您可以验证字符串是否是这样的有效时间戳:

var valid = (new Date(timestamp)).getTime() > 0;

var valid = (new Date('2012-08-09')).getTime() > 0; // true
var valid = (new Date('abc')).getTime() > 0; // false

回答by radhe_shyam

Every valid number is a timestamp. If it satisfies the condition of valid integer number then it will also satisfy the condition of the valid timestamp.

每个有效数字都是一个时间戳。如果它满足有效整数的条件,那么它也将满足有效时间戳的条件。

Timestamp = The number of milliseconds since 1970/01/01

时间戳 = 自 1970/01/01 以来的毫秒数

回答by Hristo Eftimov

The solution of @J?rgen is nice but if you have a date before January 1, 1970your timestamp will be a negative number but also a validtimestamp.

@J?rgen 的解决方案很好,但如果您在January 1, 1970时间戳之前有一个日期,它将是一个负数,但也是一个有效的时间戳。

function isValidTimestamp(_timestamp) {
    const newTimestamp = new Date(_timestamp).getTime();
    return isNumeric(newTimestamp);
}

function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

The numeric validation I took from the following SO answer.

我从以下 SO 答案中获取的数字验证

For example:

例如:

isValidTimestamp('12/25/1965') // true

回答by Srinivas

var d = Date.parse(your_timestamp);

dshould be a valid number and not NaN.

d应该是有效数字而不是 NaN。

ref: http://www.w3schools.com/jsref/jsref_parse.asp

参考:http: //www.w3schools.com/jsref/jsref_parse.asp

回答by RobG

You can't generically parse a date string without knowing beforehand what the format is, or at least that it is one of a limited number of formats.

如果事先不知道格式是什么,或者至少它是有限数量的格式之一,则无法一般地解析日期字符串。

If the date component is always in ISO8601 format (yyyy-mm-dd) and the time is either 24hr or 12hr with AM or PM, you should be able to easily split off the time, look for AM or PM, then treat the time as 12 or 24hr depending on whether it's present or not.

如果日期组件总是采用 ISO8601 格式 (yyyy-mm-dd) 并且时间是 24 小时或 12 小时(AM 或 PM),您应该能够轻松拆分时间,查找 AM 或 PM,然后处理时间12 或 24 小时取决于它是否存在。

Timezones must be specified as either UTC (Z) or hours +/-UTC, abbreviations such as EST are ambiguous (and not standardised).

时区必须指定为 UTC (Z) 或小时 +/- UTC,诸如 EST 之类的缩写不明确(且未标准化)。

回答by Gyan Chandra Srivastava

by using new Date().getTime();you can do this

通过使用 new Date().getTime();你可以做到这一点

and doing something like this

并做这样的事情

var getDate="12-12-2012";
var myDate=getDate.split("-");
var getDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(getDate).getTime());