Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const {format} = require('date-fns-tz')
const parseWithLuxon = require('.')(DateTime)
const parseWithDateFns = require('./date-fns')(dateFns)
// Europe/Berlin switched to DST at 31st of March at 2am.
const withoutDST = '2019-03-31T01:59+01:00'
const timeZone = 'Europe/Berlin'
const rel = 'in 2 minutes'
const dt = DateTime.fromISO(withoutDST).setZone(timeZone)
const withDST1 = parseWithLuxon(rel, dt)
console.log(withDST1.toFormat('HH:mm ZZZZ'))
// 03:01 GMT+2
const withDST2 = parseWithDateFns(rel, new Date(withoutDST))
console.log(format(withDST2, 'HH:mm zz', {timeZone: 'Europe/Berlin'}))
// 03:01 GMT+2
export function format(date, mask) {
if (typeof date.getFullYear !== 'function') {
throw new Error('Must pass a Date object to format it');
}
if (typeof mask !== 'string' || !mask.length) {
throw new Error(
"Invalid mask passed. Must be a string of these characters: 'dd', 'm', 'mm', 'yy', 'yyyy', 'h', 'hh', 'H', 'HH', 'M', 'MM', 's', 'ss'",
);
}
return formatDate(date, mask);
}
function getEstString(date: Date) {
const dateUTC = zonedTimeToUtc(
startOfDay(date),
Intl.DateTimeFormat().resolvedOptions().timeZone,
);
const dateEST = utcToZonedTime(dateUTC, 'EST');
return format(dateEST, 'yyyyMMdd');
}