Search code examples
node.jselectronchromiumdst

Time difference between renderer and main process


I believe this could be related to daylight saving time but not entirely sure.

Essentially i have a file that i share between both the renderer and main process.

'use strict';

const time = new Date();

module.exports = () => console.log(time);

When i require and execute in main process i get

2017-07-10T12:34:17.613Z

However when i require and execute in the renderer process i get

Mon Jul 10 2017 13:44:08 GMT+0100 (GMT Daylight Time)

When i execute node -e "console.log(new Date())" in node console i get the same output as the main process which makes sense, so i assume chromium is managing dates differently to node.

I guess the time difference and my inability to explain it points to two questions:

  • Hour difference - Why is there an hour difference? Is this some affect of DST? If so can the renderer and main process be brought into sync?
  • Second difference - Why is there also second differences? When the main and renderer process require the same file they share the same cache right? So should get the exact same date and log it?

Any help on the two points would be greatly appreciated.


Solution

  • The object returned by new Date () is the same in both contexts, but it is automatically converted into a string in two different ways.

    In the main process, console.log () probably makes use of toISOString ().

    In the renderer process, an implicit call to toString () occurs:

    The toString() method always returns a string representation of the date in American English. JavaScript calls the toString() method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.