简体   繁体   English

从JavaScript中的日期算起,当前日期(今天的日期)不得超过1年

[英]Date should not be older than 1 year from current date (Today's date) in javascript

I have a requirement where I need to find the date entered in the textfield should not be older than 1 year from current date. 我有一个要求,我需要在文本字段中输入的日期应不超过当前日期的1年。 I have coded for the former one but for the latter requirement I could not do it. 我已经为前一个编码了,但是对于后一个要求,我做不到。

   function validateNetworthDate() {

        var netWorthFlag = $("#NetworthDate").val();
        //netWorthFlag is coming( the date we entered) : "15/01/2015"

         var errorMsg='';

        var currentTime = new Date();
        var currentDay = currentTime.getDate();
        var currentMonth = currentTime.getMonth()+1; //As January is 0!
        var currentYear = currentTime.getFullYear();
        if(currentDay<10) {
            currentDay='0'+currentDay
        } 
        if(currentMonth<10) {
            currentMonth='0'+currentMonth
        } 


        var enteredDay=netWorthFlag.substr(0,2);
        var enteredMonth=netWorthFlag.substr(3,2);
        var enteredYear=netWorthFlag.substr(6,4);

        var enteredDateFormat=enteredYear+'/'+enteredMonth+'/'+enteredDay;

       //Now entered date should be : "2015/01/15"


        var enDate = new Date(enteredDateFormat);
        var compDate = currentTime - enDate;
        if (compDate<0)
        {
            errorMsg="Future Date not allowed.";
        }
        //Can you please suggest that what should I write here to do validation that Date should not be older than 1 year from current date (Today's date) 
    }

try this, after getting the string from the input, you can call a function as, 尝试一下,从输入中获取字符串后,您可以调用一个函数,

 function dateDiff(entereddate) { one_year_next_date = new Date(new Date().setYear(new Date().getFullYear() + 1)) entereddate = new Date(entereddate); return (one_year_next_date.getTime() - entereddate.getTime()) / 1000 / 60 / 60 / 24 // diff in days } var netWorthFlag = $("#NetworthDate").val(); date_difference = dateDiff(netWorthFlag); /* your input entered date */ /* eg: dateDiff("02/16/2015"); */(check date format ("02/16/2015")) 

This returns the difference in the dates as number of days. 这将以天数的形式返回日期差。

Then you can write your condition, 然后,您可以写下您的条件,

 if(date_difference > 365(/* 366 */)) /**** You can even check the leap year of entered date, or next years date ****/ { /**** do your stuff ****/ } else { /**** do your stuff ****/ } 

You can even check the leap year here, taking the year from that date.(check date format ("02/16/2015")) 您甚至可以在此处检查,年,从该日期起算年份。(检查日期格式(“ 02/16/2015”))

check it out here: 在这里查看:

https://jsfiddle.net/v63u7ksf/ https://jsfiddle.net/v63u7ksf/

    var yearAgoTime = new Date().setFullYear(currentTime.getFullYear()-1);


    var compDate2 = enDate - yearAgoTime;
    if (compDate2<0)
    {
        errorMsg="More than 1 year ago not allowed.";
    }

You can check diff yourself as suggested by others, however, I would suggest you to use moment.js for date time processing. 您可以按照其他人的建议自己检查diff,但是,我建议您使用moment.js进行日期时间处理。 Its very handy. 它非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM