js中Date()方法如何使用

242次阅读
没有评论

js中Date()方法如何使用

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电脑。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:JavaScript2022-12-15发表,共计634字。
新手QQ群:570568346,欢迎进群讨论 Python51学习