JS date方法

1. Date():获取当前时间  类型string 写参数或者不写参数 都是获取当前的时间
    例子:
    var date = Date();
    console.log(date,typeof date);//Wed Apr 03 2019 10:26:42 GMT+0800 (中国标准时间) string
    date = Date('2019-3-3');
    console.log(date);
2.new Date():获取当前时间 类型object
    var date = new Date();
    console.log(date,typeof date);//Wed Apr 03 2019 10:29:03 GMT+0800 (中国标准时间) "object"

3.可以向new Date中传入参数
    (1)①字符串参数 可以指定未来时间也可以指定过去时间
       ②参数既可以使用数字也可以是月份的简写
    var date = new Date('2019/jan/3');
    console.log(date,typeof date);
    (2)①数字传入参数 月份0开始  0代表1月
       ②需要至少放入两个参数 年份 月份
4.日期运算
    两个【对象类型的日期】相减:返回的是 日期之间的毫秒数
    var date = new Date();
    console.log(date,typeof date);
    var date1 = new Date(2019,4,1);
    console.log(date1-date,typeof (date1-date));//2380537433 "number"

5.两个【对象类型的日期】相加:两个日期拼接的字符串
    var date = new Date();
    console.log(date,typeof date);
    var date1 = new Date(2019,4,1);
    console.log(date+date1,typeof (date+date1));
    Wed Apr 03 2019 10:43:48 GMT+0800 (中国标准时间)Wed May 01 2019 00:00:00 GMT+0800 (中国标准时间) string

var date = new Date(2019,3,7);

console.log(date.getTime());//返回距离1970毫秒数
console.log(date.getYear());//当前年份距离1900年整年数 119
console.log(date.getFullYear());//当前年份距离1900年整年数 2019
console.log(date.getMonth());//3 月份从0开始
console.log('---->',date.getDay());//星期日是一周的第一天  0

date = new Date(2000,0);
var date2 = new Date(2080,0);

console.log((date2-date)/1000/60/60/24,'天');

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值