JavaScript built-in object Date (date)

JavaScript built-in object Date (date)

Date()The date object is a constructor, so you must use new to create and call the date object

var date_ = new Date(); //若括号里没有任何参数,则返回系统当前时间
console.log(date_); //Fri Nov 27 2020 23:04:45 GMT+0800 (中国标准时间)

Common writing
methods for parameters in Date() 1. Number type: 2020,05,06 (The month returned by this method is one month larger than the written month)

var date1 = new Date(2020, 05, 06);
console.log(date1); //Sat Jun 06 2020 00:00:00 GMT+0800 (中国标准时间)

2. String type (most commonly used): '2020-05-06 08:08:08'

var date2 = new Date('2020-05-06 08:08:08');
console.log(date2); //Wed May 06 2020 08:08:08 GMT+0800 (中国标准时间)

3. Date formatting year, month and day
getFullYear()—returns the year of the current date
getMonth()—the month obtained is one month smaller than the actual date , and the return value is 0~11
getDay()—returns the day of the week, but The return value on Sunday is 0
getDate()-the return is the date

var date3 = new Date();
console.log(date3.getFullYear()); //2020
console.log(date3.getMonth()); //10
console.log(date3.getDay()); //5
console.log(date3.getDate()); //27

4. Get the timestamp of the current time (total milliseconds since 1970)
Method 1: valueOf()
Method 2: getTime()
Method 3: +new Date() (Most commonly used)
Method 4: Date.now() ( HTML5 new method)

var date5 = new Date();
console.log(date5.getTime()); //1606490640866
console.log(date5.valueOf()); //1606490640866
var date6 = +new Date();
console.log(date6); //1606490640866
console.log(Date.now()); //1606490640866

5. Case (write a case in the format "Today is...year...month...day...week...")

var year = date3.getFullYear();
var month = date3.getMonth() + 1;
var day = date3.getDate();
var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
var date = date3.getDay();
console.log('今天是' + year + '年' + month + '月' + day + '日' + arr[date]); //今天是2020年11月27日星期五

Guess you like

Origin blog.csdn.net/Angela_Connie/article/details/110249883