JavaScript validate date and return last available date of calendar

前端 未结 4 820
栀梦
栀梦 2021-01-27 03:19

I want to validate my date if date not exits in calendar then return me last available date of calendar,

Example 01

Input Date    : 31         


        
4条回答
  •  迷失自我
    2021-01-27 04:03

      function isValidDate(year, month, day) {
        if(month<=12){
    
            var temp_day=day;
            var d = new Date(year, month, day);
            var lastDay = new Date(d.getFullYear(), d.getMonth(),0);
            var getlastday=lastDay.getDate();
            if(getlastday<=day){
                //var date=(d.getDate())+"/"+(d.getMonth())+"/"+(d.getFullYear());
                var date=(getlastday)+"-"+(month)+"-"+(lastDay.getFullYear());
                return date;
            }else{
                //var date=(lastDay.getDate())+"-"+(lastDay.getMonth())+"-"+(lastDay.getFullYear());
                var date=(day)+"/"+(month)+"/"+(year);
                return date;
            }
        }
        else{
            return "month not valid";
        }
    }
    

    try this code

提交回复
热议问题