Junky
Junky

Reputation: 1026

Javascript - Only allow if less than 9am EST

I have a WordPress / WooCommerce website. On the checkout page, I have a datepicker which allows the user to select a date for shipping. I allow same day shipping up to 9am EST, regardless of local time.

// Same shipping up till 9am EST
var shipTime = 0;
var currentTime = new Date();
if (currentTime.getHours() >=9){
    shipTime = 1;
} 

$( ".shipdate").datepicker( {
    minDate: shipTime
});

The problem is that the server time is UTC. I need to adjust the script so that orders placed after 9am EST are handled accordingly.

I have tried to use get getUTCHours to calculate 9am EST but i'm not so sure this is the way to do it.

// Same shipping up till 9am
// UTC time at 9am is 13
var shipTime = 0;
var currentTime = new Date();
if ( currentTime.getUTCHours() >= 13 ) {
    shipTime = 1;
}     

$( ".add_delivery_date").datepicker( {
    minDate: shipTime,  
    firstDay: 0,
    beforeShowDay: setHolidays
});

Any help to update this script would be appreciated.

Upvotes: 0

Views: 107

Answers (1)

Leo Bogod
Leo Bogod

Reputation: 419

JavaScript native Date objects only know two timezones, UTC and the user's locale timezone (and even then, the amount of information you can extract about the locale timezone is limited). You could work in UTC and subtract 4 hours to get EDT, but do you really always want EDT and not EST? i recommend you use a library I think you can try moment.js to convert between timezones

http://momentjs.com/timezone/docs/

Upvotes: 1

Related Questions