0

I am in GMT +2 timezone and Daylight saving time on.

My requirement is to ask user a date and convert it into UTC without DST consideration.

when I do console.log(new Date()), it gives me "Wed Oct 26 2016 18:00:00 GMT+0300 (Turkey Daylight Time)"

I want to send server a UTC format date which server is going to save in Database.

var extractTime = new Date(timeof.edtime); //timeof.edtime contains date given by user.
var d = extractTime.getDate();
var m = extractTime.getMonth();
var y = extractTime.getFullYear();
var h = extractTime.getHours();
var mm = extractTime.getMinutes();
timeof.edtime = moment(new Date(y, m, d, h, mm)).utc().format("YYYY-MM-DD HH:mm");

After converting to utc, timeof.edtime is "2016-09-26 15:00" because it subtract 3 hours to go to GMT. (subtracted 2 hours of standard turkish time) (subtracted -1 of DST) I want to subtract date to UTC by not considering DST and in our case my expectation is to get hour as "16:00" and not "15:00"

How to convert date to UTC without Daylight saving consideration. any solution using moment js or jquery will be helpful. I am testing on chrome.

by browsing through few link it says new Date() will give in standard time and doesn't consider DST consideration but this is not I observed and created plunk to reproduce new Date().toUTCString() consider DST as well, how to avoid subtraction of DST? https://plnkr.co/edit/tjCOoJqXMHGzCD8B5LdL?p=preview

Rahul
  • 31
  • 3
  • 8
  • Do you need the time? If not, use 12:00:00 – mplungjan Oct 26 '16 at 12:00
  • @mplungjan I didn't understood what u mean to say. I am interested in date and time both as converting to utc can change day before or after and time will definitely affect. so time and date both is important for me. – Rahul Oct 26 '16 at 12:07

2 Answers2

1

Inspired by this answer, you could make the time correction as follows:

function compensateDST(dt) {
    var janOffset = new Date(dt.getFullYear(), 0, 1).getTimezoneOffset();
    var julOffset = new Date(dt.getFullYear(), 6, 1).getTimezoneOffset();
    var dstMinutes = dt.getTimezoneOffset() - Math.max(janOffset, julOffset);
    dt = new Date(dt);
    dt.setMinutes(dt.getMinutes() - dstMinutes);
    return dt;
}

// Use your date here:
var extractTime = new Date('2016-10-26 18:00');
// Truncate to minute precision:
var extractTime = new Date(extractTime.getTime() - extractTime.getTime() % 60000)
console.log('Local time:', extractTime.toString());
console.log('UTC time before correction:', extractTime.toISOString());
// Compensate for the DST shift:
var extractTime = compensateDST(extractTime);
console.log('UTC time  after correction:', extractTime.toISOString());
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
0

try this:

+180 is GMT+0300 (Turkey)

var x = new Date();
var newdate = new Date();
if((x.getTimezoneOffset()) != 180){
 newdate = new Date(x.getTime() + (60000*(x.getTimezoneOffset()+180)));
}
console.log(newdate);
HudsonPH
  • 1,838
  • 2
  • 22
  • 38