正则表达式以验证时间戳 [英] Regular Expression to validate a timestamp

查看:162
本文介绍了正则表达式以验证时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来验证格式的时间戳,使用Java脚本:

I need a regular expression to validate a timestamp of the format, using Javascript:

YYYY/MM/DD HH:MI:SS

YYYY/MM/DD HH:MI:SS

我尝试煮一些,但是我的正则表达式技能似乎无法涵盖某些内容.

I tried cooking up a few, but seems my regex skills fail to cover something or other.

请给我一个参考或方法.

Please give me a reference or way to do it.

P.S.:我仅提及regex.我正在使用Javascript,欢迎使用其他任何替代方法.

推荐答案

我建议使用 Datejs 这.自己分析日期不是必需的,并且正则表达式不足以验证时间戳.使用datejs,您可以解析日期中的字符串,如果字符串无效,则会得到null:

I would recommend to use Datejs for this. Parsing the date yourself is not necessary and a Regex is not enough to validate a timestamp. With datejs you could parse the string in a date and you'll get null if its invalid:

Date.parse("2009/06/29 13:30:10", "yyyy/MM/dd HH:mm:ss");

这篇关于正则表达式以验证时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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