日期时间的处理

了解Date对象

一.类库推荐

1.Moment.js

二.函数处理

1.用一个函数返回当天的日期

function getCurDate(params){
    let Type = typeof(params)
    if(Type ==='string'){//设置传参只能传入字符串,因为数字和boolean都是返回19700101,对象和数组暂时没细究
        params = params
    }else{
        params = new Date()
    }
    let date = new Date(params);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = year + '' + month + '' + strDate; //什么格式自己设置一下
    return currentdate;
}
let a = getCurDate('2019')
console.log("a",a)//只传年份默认返回1月1号
console.log(new Date().getFullYear()) 
console.log(+new Date().getFullYear())//+new Date()返回时间戳,也能使用Date方法

你可能感兴趣的:(日期时间的处理)