1
0

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.

js日付チェック

Last updated at Posted at 2019-09-04
	function isDate(str) {

		// フォーマット(YYYY/MM/DD)チェック
		if (!str.match(/^\d{4}\/\d{2}\/\d{2}$/)) {
			return false;
		}

		var yyyy = str.substr(0, 4);
		var mm = str.substr(5, 2) - 1; //1月は0から始まる為 -1 する。
		var dd = str.substr(8, 2);

		// 月,日の妥当性チェック
		if (mm >= 0 && mm <= 11 && dd >= 1 && dd <= 31) {

			var vDt = new Date(yyyy, mm, dd);

			if (isNaN(vDt)) {
				return false;
			} else if(vDt.getFullYear() == yyyy && vDt.getMonth() == mm && vDt.getDate() == dd) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?