品易云推流 关闭
文章详情页
文章 > JavaScript > js中Date()方法如何使用

js中Date()方法如何使用

js Date

头像

小妮浅浅

2021-09-02 10:21:262710浏览 · 0收藏 · 0评论

1、获取当前时间必须实例化,Date对象会自动将当前日期和时间保存为初始值。

var now = new Date() ;
console. log (now) ;

2、Date构造函数的参数,如果括号中有时间,返回参数中的时间。

let endTime = '2020-01-01';
let endTime = formatDate(new Date(new Date(endTime).setDate(new Date(endTime).getDate()+1)));
console.log(endTime);//2020-01-02
 
 
//返回格式化日期 yyyy-MM-dd
function formatDate(date){
    return PrefixInteger(date.getFullYear(),4)+"-"+PrefixInteger(parseInt(date.getMonth()+1),2) +"-"+PrefixInteger(date.getDate(),2);
}
//数字转字符串,前方自动补零
function PrefixInteger(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}

以上就是js中Date()方法的使用,希望对大家有所帮助。更多js学习指路:js教程

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图