Skip to content

Instantly share code, notes, and snippets.

@ogatomo21
Created October 21, 2024 12:47
JavascriptでYYYY-MM-DD HH:MM:SSを簡単に取得するコード
const getYMD = () => {
const one2 = (num) => (num <= 9 ? `0${num}` : num);
const now = new Date();
const yy = now.getFullYear();
const mm = one2(now.getMonth()+1);
const dd = one2(now.getDate());
const h = one2(now.getHours());
const m = one2(now.getMinutes());
const s = one2(now.getSeconds());
return `${yy}-${mm}-${dd} ${h}:${m}:${s}`;
// return `${yy}-${mm}-${dd} ${h}:${m}:${s}`;
// return `${yy}-${mm}-${dd} ${h}:${m}`;
// return `${yy}-${mm}-${dd}`;
// return `${h}:${m}:${s}`;
// return `${h}:${m}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment