Thursday, 18 May 2017

Javascript date object not updated when system timezone is changed

I have this piece of nodejs code, which initially gets the current date and prints the hours and minutes. Then I change the timezone, and then again get the current date and print the hour and minutes.

'use strict';

require('shelljs/global');
// Current Timezone Asia/Kolkata
const old_date = new Date();
console.log(`Old date: ${old_date.getHours()}:${old_date.getMinutes()}`);

exec('sudo timedatectl set-timezone America/New_York', (err) => {
  if (err) { console.error(err); return }

  const new_date = new Date();
  console.log(`New date: ${new_date.getHours()}:${new_date.getMinutes()}`);
});

The output I receive is

Old date: 10:14
New date: 10:14

Apparently the javascript date object is not updated if I change the timezone, when the node process is running. Is there any workaround to get javascript to print the correct time?



via anishparanjpe

No comments:

Post a Comment