JavaScript Date(日期) 对象

1.创建 Date 对象

new Date();—— 当前日期和时间

new Date("2017-12-6 23:30:20");——参数可选

new Date(1234567891231);——距离1970年1月1日0时0分0秒的毫秒数(与中国标准时间差8个小时东八区

new Date(2017,11,6,20,30,30,300);——月份从0到11,即12月6日

2.设置Date

设置一个特定的日期:setFullYear(2017,1,14);   2017 年 2 月 14 日

setDate(myDate.getDate()+5); 如果增加天数会改变月份或者年份,那么日期对象会自动完成这种转换。

3.比较Date

var d1=new Date();
x.setFullYear(2000,5,20);
var d2 = new Date();
if(d1>d2){}
else{}


4.Date对象方法

getDate()从 Date 对象返回一个月中的某一天 (1 ~ 31)。
setDate(24)设置 Date 对象中月的某一天 (1 ~ 31)。
getDay()从 Date 对象返回一周中的某一天 (0 ~ 6)。返回值是 0(周日) 到 6(周六) 之间的一个整数。没有setDay方法
getFullYear()从 Date 对象以四位数字返回年份setFullYear(year,month,day)year参数必须,其他参数可选
getMonth()从 Date 对象返回月份 (0 ~ 11)。
getHours()返回 Date 对象的小时 (0 ~ 23)。setHours(hour,min,sec,millisec)hour参数必须,其他参数可选
getMinutes()返回 Date 对象的分钟 (0 ~ 59)。
getSeconds()返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds()返回 Date 对象的毫秒(0 ~ 999)
getTime()返回 1970 年 1 月 1 日至今的毫秒数
。。。。。。。。。。。。以上的set方法略。。。。。。。。。。。。
toString()把 Date 对象转换为字符串
toJSON()以 JSON 数据格式返回日期字符串
getTimezoneOffset()返回本地时间与格林威治标准时间 (GMT) 的分钟差

原文地址:https://www.cnblogs.com/57rongjielong/p/7995551.html