Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Created October 18, 2017 14:03
Show Gist options
  • Save ryasmi/e76f7c1be481e736aa18c1996053c17a to your computer and use it in GitHub Desktop.
Save ryasmi/e76f7c1be481e736aa18c1996053c17a to your computer and use it in GitHub Desktop.
pad = (num) => {
return num < 10 ? `0${num}` : num.toString();
};
getIsoDate = (date) => {
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const monthDay = pad(date.getDate());
return `${year}-${month}-${monthDay}`;
};
getIsoTime = (date) => {
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
const milliseconds = pad(date.getMilliseconds());
return `${hours}:${minutes}:${seconds}.${milliseconds}`;
};
getIsoOffset = (date) => {
const offset = date.getTimezoneOffset();
if (offset === 0) {
return 'Z';
}
const absOffset = Math.abs(offset);
const offsetHours = pad(absOffset / 60);
const offsetMinutes = pad(absOffset % 60);
const offsetSign = offset > 0 ? '-' : '+';
return `${offsetSign}${offsetHours}:${offsetMinutes}`;
};
getTimestamp = () => {
const date = new Date();
const isoDate = getIsoDate(date);
const isoTime = getIsoTime(date);
const isoOffset = getIsoOffset(date);
return `${isoDate}T${isoTime}${isoOffset}`;
};
getTimestamp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment