简体   繁体   中英

Javascript Date Object Timezone issue - different timezones

I'm using a date/time picker with a "friendly" display for the input value which I am trying to convert to a javascript date object. When the date is selected, it comes in this format:

October 21 2013 09:00

Which I then attempt to covert to a javascript object with the following function:

function pickerDateToJavascriptObj(dateIn)
{
    dateIn = dateIn.split(/[- :]/);
    dateIn[0] = getMonthNumber(dateIn[0]);
    outDate = new Date(dateIn[2],dateIn[0],dateIn[1],dateIn[4],dateIn[5], "00");
    console.log(outDate);
    return outDate; 
}

I have to date pickers on my page a 'from' date and a 'to' date. When I submit the form and console.log the outDates, the dates are correctly converted to an object, however they consistently have different time zones. For the 'from' date, I get in my console:

Date {Mon Oct 21 2013 09:00:00 GMT+0100 (GMT Standard Time)}

and for the 'to' date, I am getting:

Date {Thu Oct 31 2013 09:00:00 GMT+0000 (GMT Daylight Time)}

Notice the difference between standard and daylight time of the 2 dates.

What could be causing this discrepancy?

Here are the 2 inputs:

<input type="text" placeholder="Start Date/Time" name="eventStart" id="eventStart" class="dateTimePicker" readonly="readonly">
<input type="text" placeholder="End Date/Time" name="eventEnd" id="eventEnd" class="dateTimePicker" readonly="readonly">

It looks like you are in the UK and using Firefox.

Daylight Saving Time (aka "British Summer Time") ends on Oct 27th , so your start date is before the switch and your end date is after.

You are also witnessing a bug in FireFox because while the offsets are correct, the names are inverted.

Get the date formatted like this (instead of parsing manually):

var myDate= $("#datepicker").datepicker({ dateFormat: 'dd-mm-yy' }).val();

API Doc: http://api.jqueryui.com/datepicker/#option-dateFormat

Get the time formatted like this:

var myTime = $.datepicker.parseTime('HH:mm:ss', $("#datepicker").val());

API Doc: http://trentrichardson.com/examples/timepicker/#tp-formatting

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