日期闰年验证 [英] Date leap year validation

查看:156
本文介绍了日期闰年验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function checkDate(date)
{
//如何使用这个函数,因为很多人推荐这个函数
isLeap = new Date(date,1,29)。 getMonth()== 1;
return isLeap;

}

在html脚本中,我写了,但我可以'验证我的订单日期。我需要使用javascript,它应该能够验证它包括闰年。以上使用的功能无法正常工作。任何帮助将不胜感激。

解决方案

检查是否是闰年:



var isleap =(year%4 == 0&&(year%100!= 0 || year%400 == 0)); p>

检查额外的一天是否有效 - 是检查该月份有多少天(如果等于dd):



32 - new Date(2000,1,32).getDate() // 1这里是FEB



如果有人说: 29 feb 2000



so
32 - new Date(2000,1,32).getDate() // 29



cuz 29 == 29。



但是如果有人说:$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 2001



32 - 新日期(2001,1,32).getDate() // 28



和28!= 29,因此无效日期。


function checkDate(date)
{
        //how to use this function since many people recommend this one
    isLeap = new Date(date, 1, 29).getMonth() == 1;
    return isLeap;

}

In html script, i wrote it as but i can't validate my Date of Order. I need to use javascript and it should be able to validate it including leap year. The function I used above can't work. Any helps will be appreciated.

解决方案

To check if it's leap year :

var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

to check if the extra day is valid -- is to check how many days were in that month (and if they equal to dd):

32 - new Date(2000, 1, 32).getDate() //1 here is FEB

do if some says : 29 feb 2000

so 32 - new Date(2000, 1, 32).getDate()//29

which is fine . cuz 29==29.

BUT

do if some says : 29 feb 2001

32 - new Date(2001, 1, 32).getDate()//28

and 28!=29 so it's not valid date.

这篇关于日期闰年验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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