🦁
【JavaScript】現在日時の取得方法
const newDate = new Date();
const week = ["日","月","火","水","木","金","土"];
const newDateYear = newDate.getFullYear() + "年";
const newDateMonth = (newDate.getMonth() + 1) + "月";
const newDateDate = newDate.getDate() + "日";
const newDateHour = newDate.getHours() + "時";
const newDateMinutes = newDate.getMinutes() + "分";
const newDateSeconds = newDate.getSeconds() + "秒";
const newDateMilliseconds = newDate.getMilliseconds() + "ミリ秒";
const newDateWeek = week[newDate.getDay()] + "曜日";
ちなみに yyyy/mm/dd
で取得したいときは以下のようにします。
const nowYear = newDate.getFullYear();
const nowMonth = ("00" + (newDate.getMonth() + 1)).slice(-2);
const nowDate = ("00" + (newDate.getDate())).slice(-2);
const nowYearMonthDate = nowYear + nowMonth + nowDate;
上記の nowYearMonthDate
は文字列になっているので、数値型に変換するには以下のように Number()
を使用することで可能となります。
Number(nowYearMonthDate);
Discussion