Basic Syntax
Declare a variable to store date data.
1
let time=new Date(); // get the current time data
- Get year:
let year = time.getFullYear();
- Get month:
let month=time.getMonth()+1;
- Be aware that for month data, it will count from 0.
- Get weekday:
let weekday=time.getDay();
- Be aware that for weekday data, it will count from 0.
- Get hour:
let hour=time.getHours();
- Get minute:
let minute=time.getMinutes();
- Get second:
let second=getSeconds();
- Get milliseconds:
let time.getMilliseconds();
- Get time stamp:
let stamp=time.getTime();
Additional Knowledge
- JavaScript Date Time Method 日期時間
- 前端工程研究:關於 JavaScript 中 Date 型別的常見地雷與建議作法
- 補零應用的技巧
Demo
- Get year: