2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

日付時刻として正しいか判定する正規表現(JavaScript)

Last updated at Posted at 2018-06-28

YmdHis形式で保存した日付時刻が正しいか検証する正規表現(JavaScript用)を書きました。
もっと短くできると思いますが、自分の力では無理なので、何か方法あったら教えてくださいm(_ _)m

let re = /^((([02468][048]|[13579][26])00|[0-9]{2}([2468][048]|[13579][26]|0[48]))(02(0[1-9]|1[0-9]|2[0-9]))|([0-9]{4}((0[1-9])(0[1-9]|1[0-9]|2[0-8])|(0[1[3-9]]|1[0-2])(0[1-9]|[12][0-9]|30)|(0[13578]|12)31)))([01][0-9]|2[0-3])[0-5][0-9]([0-5][0-9]|60)$/;
re.test('20180628114959') // => true

規則的に入る閏年の対応はできましたが、閏秒に関しては不定期ではいるので、毎分の「60秒」を正しい時刻として判定する仕様になっています。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?