Skip to content

Instantly share code, notes, and snippets.

@starfeeling
Created February 14, 2018 01:51
Show Gist options
  • Save starfeeling/a626dfd73c826437a9cda60747e9ee5a to your computer and use it in GitHub Desktop.
Save starfeeling/a626dfd73c826437a9cda60747e9ee5a to your computer and use it in GitHub Desktop.
function format(val, figure) {
return val < 10 * (figure - 1) ? `0${val}` : `${val}`;
}
function dateformat(dateObject) {
const date = dateObject || new Date();
const year = format(date.getFullYear(), 4);
const month = format(date.getMonth() + 1, 2);
const day = format(date.getDate(), 2);
const hour = format(date.getHours(), 2);
const minute = format(date.getMinutes(), 2);
const second = format(date.getSeconds(), 2);
return {
year,
month,
day,
hour,
minute,
second
};
}
module.exports = dateformat;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment