0

I am trying to check for a expired date and time logic. The time is in 12 hour clock format. I want to check if date is today, then user should not be able to pick expired time. However, if the date selected is tomorrow, then any time can be selected.If date is yesterady, then user should not be able to select the date.

I am trying to do a check in jquery, but not sure how to check. The date is in the format of "MM/DD/YYYY", and the time is in format of "hh:mm a". Expired time of 5 minutes is allowed. I have tried this code:

     var targetTime = new Date().setMinutes(-5).valueOf();

            var today = new Date();
            var dd = today.getDate();
            var mm = today.getMonth() + 1; //January is 0!
            var yyyy = today.getFullYear();

            if (dd < 10) {
                dd = '0' + dd
            }

            if (mm < 10) {
                mm = '0' + mm
            }

            today = mm + '/' + dd + '/' + yyyy;

            if (jQuery('#startDatepicker').find("input").val() == today) {

                var currentStartDate = jQuery('#startDatepicker').find("input").val();
                var currentStartTime = jQuery('#startTimepicker').find("input").val();

                var UserSelectedTime = getAsDate(currentStartDate, currentStartTime).getTime();

                if (UserSelectedTime <= targetTime) {
                    alert("Start Time has expired. Please select a valid Start Time");
                }
            }

 function getAsDate(day, time) {
        var hours = Number(time.match(/^(\d+)/)[1]);
        var minutes = Number(time.match(/:(\d+)/)[1]);
        var AMPM = time.match(/\s(.*)$/)[1];
        if (AMPM == "pm" && hours < 12) hours = hours + 12;
        if (AMPM == "am" && hours == 12) hours = hours - 12;
        var sHours = hours.toString();
        var sMinutes = minutes.toString();
        if (hours < 10) sHours = "0" + sHours;
        if (minutes < 10) sMinutes = "0" + sMinutes;
        time = sHours + ":" + sMinutes + ":00";

        var d = new Date(day);
        var n = d.toISOString().substring(0, 10);
        var newDate = new Date(n + "T" + time);
        return newDate;
    }

If the current date is today and If the current time is 4:00 AM, if the user has selected 3:00 am , then the time has expired, but the alert message is not showing. How to fix this? Thanks

venkat14
  • 453
  • 2
  • 10
  • 24
  • is a `regex` question? if yes, maybe [here](http://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy) – ramabarca Feb 08 '17 at 13:28
  • Hi. Not a regex question. I am looking to validate the expired time for the current date using jQuery/Javascript – venkat14 Feb 09 '17 at 04:37

0 Answers0