sectom
Version:
Sectom is a useful npm package that has multiple easy to use functions.
39 lines (37 loc) • 766 B
JavaScript
/**
*
* @param {Date} d
* @param {number} offset
* @returns {string}
*/
function formatDate(d, offset) {
function z(n) {
return ("0" + n).slice(-2);
}
// Default offset to 0
offset = offset || 0;
// Generate offset string
var offSign = offset < 0 ? "-" : "+";
offset = Math.abs(offset);
var offString =
offSign +
("0" + ((offset / 60) | 0)).slice(-2) +
":" +
("0" + (offset % 60)).slice(-2);
// Generate date string
return (
d.getUTCFullYear() +
"-" +
z(d.getUTCMonth() + 1) +
"-" +
z(d.getUTCDate()) +
"T" +
z(d.getUTCHours()) +
":" +
z(d.getUTCMinutes()) +
":" +
z(d.getUTCSeconds()) +
offString
);
}
module.exports = formatDate;