1、getDate() 方法根据本地时间,返回一个指定的日期对象为一个月中的哪一天。
返回1-31的整数值。
例子:
var bir = new Date();
console.log(bir.getDate());//7
var day = new Date("May 16,2020 08:29:30");
console.log(day.getDate());//16
2、 getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0表示星期天。
例子:
var day = new Date("May 16,2020 08:29:30");
console.log(day.getDay());//6,表示星期六
//获取当前的星期
var week = new Date();
var weeks =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
console.log(weeks[week.getDay()]);//星期一
3、getFullYear() 方法根据本地时间返回指定日期的年份。
该方法返回的是绝对数。对于1000-9999之间的日期,此方法返回的是一个四位数。
例子:
//var day = new Date("May 16,2020 08:29:30");//2020
var day = new Date("May 16,89 08:29:30");//1989
console.log(day.getFullYear());
4、getHours() 方法返回一个指定的日期对象的小时。
例子:
var day = new Date("May 16,12 08:29:30");
console.log(day.getHours());//8
var bir = new Date();
console.log(bir.getHours());//19
5、getMilliseconds() 方法返回一个指定的日期对象的毫秒数。
返回一个0-999之间的整数
例子:
var day = new Date("May 16,12 08:29:30:268");
console.log(day.getMilliseconds());//268
var bir = new Date();
console.log(bir.getMilliseconds());//643
6、getMinutes() 方法返回一个指定的日期对象的分钟数。
返回0-59的整数。
例子:
var day = new Date("May 16,12 08:29:30");
console.log(day.getMinutes());//29
var bir = new Date();
console.log(bir.getMinutes());//14
7、getMonth() 方法返回一个指定的日期对象的月份,为基于0的值(0表示一年中的第一个月)
返回值为0-11
例子:
var day = new Date("May 16,12 08:29:30");
console.log(day.getMonth());//4
var bir = new Date();
console.log(bir.getMonth());//5
8、getSeconds() 方法返回一个指定的日期对象的秒数。
返回值0-59之间
例子:
var day = new Date("May 16,12 08:29:30");
console.log(day.getSeconds());//30
var bir = new Date();
console.log(bir.getSeconds());//48
9、getTime() 方法返回距1970年1月1日之间的毫秒数。
返回一个数值,表示从1970年1月1日0时0分0秒(UTC,即协调世界时间时)距离该日期对象所代表的时间的毫秒数。
例子:
var day = new Date("May 16,12 08:29:30");
console.log(day.getTime());//1337128170000
var bir = new Date();
console.log(bir.getTime());//1623066417884
10、getTimezoneOffset() 方法返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。
注意:如果本地时区先于协调世界时(世界标准时间),则该差值为正值,如果后于协调世界时则为负值。
例子:
var date1 = new Date('May 19,1975 23:15:30 GMT+07:00');
var date2 = new Date('May 19,1975 23:15:30 GMT-02:00');
console.log(date1.getTimezoneOffset());//-480
console.log(date1.getTimezoneOffset() === date2.getTimezoneOffset());//true
11、parse() 方法解析一个表示某个日期的字符串,并返回从1970-1-1 0时0分 UTC到该日期对象(该日期对象的UTC时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包含了不合法的日期数值(如 2010-2-30),则返回NaN。
语法:
显式调用:Date.parse(date);
隐式调用:new Date(date).getTime();
例子:
var date1 = Date.parse('May 19,1975 23:15:30 GMT+07:00');
var date2 = Date.parse('May 19,1975 23:15:30 GMT-02:00');
var date3 = new Date('May 19,1975 23:15:30 GMT+07:00').getTime();
console.log(date3);//169748130000
console.log(date1);//169748130000
console.log(date2);//169780530000
console.log(date1 === date2);//false
12、now() 方法返回自1970年1月1日 0时0分(UTC)到当前时间的毫秒数。
例子:
var day = Date.now();
console.log(day);//1623067594497