Javascript 判断输入日期是否正确

JavaScript的Date对象有容错性,可将随意给定的日期的年月日自动生成正确的日期时间


//JavaScript中Date对象容错性
function dateCheck(){
var date = new Date();
date.setDate(date.getDate()+13);
//date.setDate(date.getMonth()+1+10);
//打印依然能输出正确的日期
console.log(date.getFullYear()+"年"+(date.getDate())+"日"+(date.getMonth()+1)+"月");
}

由此可以根据Date对象的容错性对输入日期的年月日进行正确性判断,具体代码如下:


//判断给定日期是否合法
function checkDate(year,month,date){
var now = new Date(year,month -1,date);
if(now.getDate()===date&&now.getFullYear()==year&&now.getMonth()===(month-1)){
return true;
}
return false;
}
checkDate(2014,8,34);

Javascript 判断输入日期是否正确

时间: 2024-08-07 20:53:33

Javascript 判断输入日期是否正确的相关文章

JavaScript判断输入的内容是否为空,应该注意的一点!

JavaScript 我们经常要判断一个输入框中是否输入了内容,如果未输入内容就要弹出提示框告诉用户未输入内容! 但是这里有一个问题,如果用户输入的是一个空格 或者是一些其它空字符,这时如果不注意,就达不到要求了! 下面我给我出一段JavaScript代码用来解决以上问题: Js代码   //提交查询内容 function formSubmit() { var condId = document.getElementById("searchProductText"); var text

javascript 判断str日期是否是今天,参数diff

function isToday(str) { var d = new Date(str.replace(/-/g, "/")); var todaysDate = new Date(); if (d.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0)) { return true; } else { return false; } } by default7#zbphp.com 自己修改,增加diff参数. diff为天数

输入年月日, 判断输入的是否正确

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 条件语句日期练习 { class Program { static void Main(string[] args) { //用户输入年.月.日,判断用户输入的年月日是否正确 //用户输入年份 Console.Write("请输入年份(0-9999):"); int y = Convert.ToI

JavaScript 判断输入是否为中文的函数

//---------------------------------------------------------- // 功能:判断输入是否为中文的函数 // 参数: // s // 返回值: // true 符合中文格式 // false 不符合中文码格式 //---------------------------------------------------------- function CheckIsChinese(s){ var ret=true; for(var i=0;i<

JavaScript判断输入的日期是今年的第几天

const readline = require("readline-sync"); console.log("请输入一个年份:"); let year = readline.question()-0; while (isNaN(year) || year < 1 || year > 2018) { console.log("年份输入有误"); year = readline.question()-0; } console.log(&

HQ-day4 C#语句实例⑤判断一个日期是否正确

Python中判断是否为闰年,求输入日期是该年第几天

#coding = utf-8 def getLastDay(): y = int(input("Please input year :")) m = int(input("please input month :")) d = int(input("Please input day :")) s=0 if y <1: y=1 if m <1: m=1 if m>12: m=12 if d <1: d=1 mothday=

3月11日 判断输入的年月日是否正确

//判断年月日是否正确: for (; ; ) { Console.Write("请输入年份:"); int a = int.Parse(Console.ReadLine()); if (a >= 0 && a <= 9999) { Console.Write("请输入月份:"); for (; ; ) { int b = int.Parse(Console.ReadLine()); if (b <= 12 && b

Html5页面使用javascript setCutomValidity()函数验证表单判断输入

<!DOCTYPE HTML><head><meta charset="UTF-8"><title>Html5页面使用javascript验证表单判断输入</title><script language="javascript">function check(){    var pass1=document.getElementbyid("pass1");    var pa