Skip to content Skip to sidebar Skip to footer

Calculating If Date Picker + Time Picker Are In The Past Of Eastern Standard Time Zone

I've got a standard input type=date field that outputs a value such as '2016-03-28' when its posted, and a time picker that outputs a value like '10:30pm' when posted. I need to ch

Solution 1:

Here's how I did it:

var entryDate = $("input[name='launchDate']").val();
        var entryTime = $("input[name='launchtime']").val();
        var morningEvening = entryTime.slice(-2);
        var fixedTime;
        if (morningEvening === "am") {
          fixedTime = entryTime.split('am').join(' AM');
        } else {
          fixedTime = entryTime.split('pm').join(' PM');
        }

        var newDate = newDate(entryDate + " " + fixedTime);
        var inputDate = newDate.toLocaleString();
        var offset = -5.0;

        var clientDate = newDate();
        var utc = clientDate.getTime() + (clientDate.getTimezoneOffset() * 60000);

        var newYork = newDate(utc + (3600000*offset));
        var newYorkDate = newYork.toLocaleString();

        if (inputDate < newYorkDate) {
          console.log("It's in the past");
        } else {
          console.log("It's in the future");
        }

Solution 2:

Given separate inputs for date like 2016-03-28 and time like 10:30pm you should parse both to produce a Date. To treat the date as EST (presumably US -05:00), create the date using UTC methods and add 5 hours.

A Date for "now" will have a UTC time value so you can use that as–is, then just directly compare the Date objects:

/* @param {string} ds - date string in format yyyy-mm-dd
** @param {string} ts - time string in format hh:mmap
** @returns {Date} Date with time value for equivalent EST time
*/functionparseDateTimeAsEST(ds, ts) {
  var b = ds.split(/\D/);
  var c = ts.split(/\D/);
  c[0] = +c[0] + (/pm$/i.test(ts)? 12 : 0);
  returnnewDate(Date.UTC(b[0], b[1]-1, b[2], c[0]+5, c[1]));
}

// Is now before or after 2016-03-28T22:30:00-05:00?document.write(newDate() < parseDateTimeAsEST('2016-03-28','10:30pm')? 'before' : 'after');

document.write('<br>');

// Is now before or after 2016-04-01T01:30:00-05:00?document.write(newDate() < parseDateTimeAsEST('2016-04-01','01:30am')? 'before' : 'after');

Post a Comment for "Calculating If Date Picker + Time Picker Are In The Past Of Eastern Standard Time Zone"