Regular Expression to validate a timestamp(正则表达式验证时间戳)
问题描述
我需要一个正则表达式来验证格式的时间戳,使用 Javascript:
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.
附注: 我提到正则表达式,只是作为一个建议.我使用 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");
这篇关于正则表达式验证时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:正则表达式验证时间戳
基础教程推荐
- 在 contenteditable 中精确拖放 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
