简体   繁体   中英

In javascript how to check current date with three dates

In Javascript how to check current datetime with three dates. i want to disable the radio button if date is pervious datetime.i need to validate the date according to current datetime suppose a)10/2/2017 11:00 b)21/3/2018 11:20 c)28/4/2018 14:00 above three dates i need to block the radio button and stike the date in javascript

 function formatAMPM(date) { // This is to display 12 hour format like 
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return strTime;
        }


        var myDate = new Date();
        var displayDate = myDate.getMonth()+ '/' +myDate.getDate()+ '/' +myDate.getFullYear()+ ' ' +formatAMPM(myDate);
        alert(displayDate);
        var dt = Date.parse(displayDate);
        var d = Date.parse(ts);
        var d1 = Date.parse(ts1);
        var d2 = Date.parse(ts2);
        alert(d);
        alert(dt);

There is a good library momemt you can use that to check whether date is current date. You can use isBefore

Also if you want to use JS only you can do the following:

Convert your date string in Date object and get the time in milliseconds and then compare it to Date.now().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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