5

To create Date object in UTC, we would write

new Date(Date.UTC(2012,02,30));

Without Date.UTC, it takes the locale and creates the Date object. If I have to create a Date object for CET running the program in some part of the world, how would I do it?

Nandish A
  • 1,645
  • 5
  • 26
  • 41

2 Answers2

3

You don't create a JavaScript Date object "in" any specific timezone. JavaScript Date objects always work from a milliseconds-since-the-Epoch UTC value. They have methods that apply the local timezone offset and rules (getHours as opposed to getUTCHours), but only the local timezone. You can't set the timezone the Date object uses for its "local" methods.

What you're doing with Date.UTC (correctly, other than the leading 0 on 02) is just initializing the object with the appropriate milliseconds-since-the-Epoch value for that date/time (March 30th at midnight) in UTC, whereas new Date(2012, 2, 30) would have interpreted it as March 30th at midnight local time. There is no difference in the Date object other than the datetime it was initialized with.

If you need a timezone other than local, all you can do is use the UTC version of Date's functions and apply your own offset and rules for the timezone you want to use, which is non-trivial. (The offset is trivial; the rules tend not to be.)

If you go looking, you can find Node modules that handle timezones for you. A quick search for "node timezone" just now gave me timezone as the first hit. It also gave me links to this SO question, this SO question, and this list of timezone modules for Node.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the links. I used the following code `require('timezone-js'); var dt = new timezoneJS.Date('2012, 06, 8, 11, 55, 4','Europe/Amsterdam');` and I get the following error `ReferenceError: timezoneJS is not defined`. I ran `npm install timezone-js` before executing the script – Nandish A Jan 01 '14 at 10:58
  • @NandishA: I'm glad this helped. As for the issue with `timezone-js`, that would be an unrelated question. If you can't figure it out from the `timezone-js` and Node docs, post a new question about it. (I don't think you want those numbers in the first argument to be in a string, but of course that's not why you're getting that error.) – T.J. Crowder Jan 01 '14 at 11:00
  • Thanks, I have created a new question http://stackoverflow.com/questions/20867990/using-timezone-js-in-nodejs-program – Nandish A Jan 01 '14 at 11:06
3
function getCETorCESTDate() {
    var localDate = new Date();
    var utcOffset = localDate.getTimezoneOffset();
    var cetOffset = utcOffset + 60;
    var cestOffset = utcOffset + 120;
    var cetOffsetInMilliseconds = cetOffset * 60 * 1000;
    var cestOffsetInMilliseconds = cestOffset * 60 * 1000;

    var cestDateStart = new Date();
    var cestDateFinish = new Date();
    var localDateTime = localDate.getTime();
    var cestDateStartTime;
    var cestDateFinishTime;
    var result;

    cestDateStart.setTime(Date.parse('29 March ' + localDate.getFullYear() + ' 02:00:00 GMT+0100'));
    cestDateFinish.setTime(Date.parse('25 October ' + localDate.getFullYear() + ' 03:00:00 GMT+0200'));

    cestDateStartTime = cestDateStart.getTime();
    cestDateFinishTime = cestDateFinish.getTime();

    if(localDateTime >= cestDateStartTime && localDateTime <= cestDateFinishTime) {
        result = new Date(localDateTime + cestOffsetInMilliseconds);
    } else {
        result = new Date(localDateTime + cetOffsetInMilliseconds);
    }

    return result;
}
  • This would always result in UTC+1. But isn't CET either UTC+1 in winter or UTC+2 in summer? – Moby Disk Jun 23 '15 at 15:35
  • Date object have a getMonth and getDay methods, and you can calculate it. I think this is no problem. – Sergey Krivtsov Jun 25 '15 at 13:23
  • @MobyDisk, I edit my code for detect clock transitions beetwen CET and CSET. Enjoy! Thank you for you comment, in first time, I didn't thought through this point. – Sergey Krivtsov Jun 28 '15 at 10:34
  • @SergeyKrivtsov cestDateStart.setTime(Date.parse('29 March ' + localDate.getFullYear() + ' 02:00:00 GMT+0100')); cestDateFinish.setTime(Date.parse('25 October ' + localDate.getFullYear() + ' 03:00:00 GMT+0200')); Can you please explain this line? – Manush May 08 '18 at 07:37
  • @SergeyKrivtsov As per docs they just mentioned '02:00 am on the last Sunday of March until 03:00 am on the last Sunday of October.' but how you gave here specific dates like 29th march and 25th October. Can u plz explain? – Roy Oct 05 '20 at 10:57
  • @Manush Do you understand now, which you asked above comment? – Roy Oct 05 '20 at 11:02