UNPKG

sectom

Version:

Sectom is a useful npm package that has multiple easy to use functions.

33 lines (29 loc) 1.05 kB
const formatDate = require("../private/formatDate"); /** * Finds the time in london for the current date or a provided date * @param {Date} d * @returns */ function getLondonTime(d = undefined) { // Use provided date or default to current date and time d = d || new Date(); // Get start and end dates for daylight saving for supplied date's year // Set UTC date values and time to 01:00 var dstS = getLastSunday(d.getFullYear(), 3); var dstE = getLastSunday(d.getFullYear(), 10); dstS = new Date( Date.UTC(dstS.getFullYear(), dstS.getMonth(), dstS.getDate(), 1) ); dstE = new Date( Date.UTC(dstE.getFullYear(), dstE.getMonth(), dstE.getDate(), 1) ); // If date is between dstStart and dstEnd, add 1 hour to UTC time // and format using +60 offset if (d > dstS && d < dstE) { d.setUTCHours(d.getUTCHours() + 1); return formatDate(d, 60); } // Otherwise, don't adjust and format with 00 offset return formatDate(d).toString(); } module.exports = getLondonTime;