Skip to content

Instantly share code, notes, and snippets.

@LeonanCarvalho
Last active March 11, 2022 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonanCarvalho/35d1596dcfb701255d04b93d70df69a0 to your computer and use it in GitHub Desktop.
Save LeonanCarvalho/35d1596dcfb701255d04b93d70df69a0 to your computer and use it in GitHub Desktop.
DayJs bug at dayjs.tz()
//Changing the TZ also change results.
//process.env.TZ = 'UTC';
//process.env.TZ = 'America/Sao_Paulo'; //BUG: Returning with DST, but since 2018 Brazil didn't have it.
//process.env.TZ = 'America/Recife'; //Workaround: Using recife because it hasn't DST previously.
//PS: may the bug with TZ is a SO/nodeJS related
const now = new Date();
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
dayjs.extend(timezone);
dayjs.extend(utc);
dayjs.tz.setDefault('America/Recife'); //GMT -3
dayjs.locale('pt-br');
let dates = [
now,
now.toISOString(),
now.toUTCString(),
now.getTime(),
now.toLocaleDateString(),
now.toLocaleString(),
'2004-11-02',
'2012-11-02',
'2014-02-03T16:50:21Z',
'2012-02-01T13:50:21.01-03:00',
'2022-03-09T10:12:58.001Z',
'2022-02-03T13:50:21-00:00'
];
for (let d of dates) {
const ds = dayjs.tz(d, 'America/Recife');
console.info(`
Input(${typeof d}): ${d}
Result (GMT -3): ${ds.format()}
`);
console.info('raw:', ds);
}

Output:

Now: Fri Mar 11 2022 14:29:26 GMT+0000 (Coordinated Universal Time)
Timezone: UTC

Input(object):           Fri Mar 11 2022 14:29:26 GMT+0000 (Coordinated Universal Time)
Result (GMT -3):              2022-03-11T11:29:26-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T11:29:26.319Z,
  '$x': { '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 11,
  '$m': 29,
  '$s': 26,
  '$ms': 319,
  '$offset': -180,
  '$u': false
}

Input(string):           2022-03-11T14:29:26.319Z
Result (GMT -3):              2022-03-11T14:29:26-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T14:29:26.319Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 14,
  '$m': 29,
  '$s': 26,
  '$ms': 319,
  '$offset': -180
}

Input(string):           Fri, 11 Mar 2022 14:29:26 GMT
Result (GMT -3):              2022-03-11T14:29:26-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T14:29:26.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 14,
  '$m': 29,
  '$s': 26,
  '$ms': 0,
  '$offset': -180
}

Input(number):           1647008966319
Result (GMT -3):              2022-03-11T11:29:26-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T11:29:26.319Z,
  '$x': { '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 11,
  '$m': 29,
  '$s': 26,
  '$ms': 319,
  '$offset': -180,
  '$u': false
}

Input(string):           3/11/2022
Result (GMT -3):              2022-03-11T00:00:00-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T00:00:00.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 0,
  '$m': 0,
  '$s': 0,
  '$ms': 0,
  '$offset': -180
}

Input(string):           3/11/2022, 2:29:26 PM
Result (GMT -3):              2022-03-11T14:29:26-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T14:29:26.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 14,
  '$m': 29,
  '$s': 26,
  '$ms': 0,
  '$offset': -180
}

Input(string):           2004-11-02
Result (GMT -3):              2004-11-02T00:00:00-03:00

raw: M {
  '$L': 'en',
  '$d': 2004-11-02T00:00:00.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2004,
  '$M': 10,
  '$D': 2,
  '$W': 2,
  '$H': 0,
  '$m': 0,
  '$s': 0,
  '$ms': 0,
  '$offset': -180
}

Input(string):           2012-11-02
Result (GMT -3):              2012-11-02T00:00:00-03:00

raw: M {
  '$L': 'en',
  '$d': 2012-11-02T00:00:00.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2012,
  '$M': 10,
  '$D': 2,
  '$W': 5,
  '$H': 0,
  '$m': 0,
  '$s': 0,
  '$ms': 0,
  '$offset': -180
}

Input(string):           2014-02-03T16:50:21Z
Result (GMT -3):              2014-02-03T16:50:21-03:00

raw: M {
  '$L': 'en',
  '$d': 2014-02-03T16:50:21.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2014,
  '$M': 1,
  '$D': 3,
  '$W': 1,
  '$H': 16,
  '$m': 50,
  '$s': 21,
  '$ms': 0,
  '$offset': -180
}

Input(string):           2012-02-01T13:50:21.01-03:00
Result (GMT -3):              2012-02-01T16:50:21-03:00

raw: M {
  '$L': 'en',
  '$d': 2012-02-01T16:50:21.010Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2012,
  '$M': 1,
  '$D': 1,
  '$W': 3,
  '$H': 16,
  '$m': 50,
  '$s': 21,
  '$ms': 10,
  '$offset': -180
}

Input(string):           2022-03-09T10:12:58.001Z
Result (GMT -3):              2022-03-09T10:12:58-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-09T10:12:58.001Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 9,
  '$W': 3,
  '$H': 10,
  '$m': 12,
  '$s': 58,
  '$ms': 1,
  '$offset': -180
}

Input(string):           2022-02-03T13:50:21-00:00
Result (GMT -3):              2022-02-03T13:50:21-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-02-03T13:50:21.000Z,
  '$x': { '$localOffset': 0, '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 1,
  '$D': 3,
  '$W': 4,
  '$H': 13,
  '$m': 50,
  '$s': 21,
  '$ms': 0,
  '$offset': -180
}

Input(object):           Fri Mar 11 2022 14:29:26 GMT+0000 (Coordinated Universal Time)
Result (GMT -3):              2022-03-11T11:29:26-03:00

raw: M {
  '$L': 'en',
  '$d': 2022-03-11T11:29:26.319Z,
  '$x': { '$timezone': 'America/Recife' },
  '$y': 2022,
  '$M': 2,
  '$D': 11,
  '$W': 5,
  '$H': 11,
  '$m': 29,
  '$s': 26,
  '$ms': 319,
  '$offset': -180,
  '$u': false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment