Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Created August 28, 2021 01:51
Show Gist options
  • Save zazaulola/30df6a33f18b00368bfe00f986093855 to your computer and use it in GitHub Desktop.
Save zazaulola/30df6a33f18b00368bfe00f986093855 to your computer and use it in GitHub Desktop.
/**
* Convert DateTime to timestamp
* @param {Number} y Full year
* @param {Number} M Month (1..12)
* @param {Number} d Date (1..31)
* @param {Number} h Hour (0..23)
* @param {Number} m Minute (0..59)
* @param {Number} s Second (0..59)
* @param {Number} l Millisecond (0..999)
* @oaram {Number} z Time zone
* @returns {Number}
*/
function date2ts([y, M, d, h, m, s, l, z]) {
const date = new Date();
date.setFullYear(y);
date.setMonth(M - 1);
date.setDate(d);
date.setHours(h);
date.setMinutes(m);
date.setSeconds(s);
if (l) {
date.setMilliseconds(l);
}
if (z) {
date.getTimezoneOffset(z);
}
return date.getTime();
}
/**
* Convert Timestamp to DateTime
* @param {Number} t Unix timestamp
* @returns {Number[]}
*/
function ts2date(t) {
const date = new Date(t);
const y = date.getFullYear();
const M = date.getMonth() + 1;
const d = date.getDate();
const h = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
const l = date.getMilliseconds();
const z = date.getTimezoneOffset();
return [y, M, d, h, m, s, l, z];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment