js 时间获取

先看看如何获取时间:

< script>
window. onload = function(){
( function abc(){
var date =new Date();
console. log(date. getFullYear()); // 获取年份
console. log(date. getMonth() + 1); // 获取月份
console. log(date. getDate()); // 获取日
console. log(date. getDay()); // 获取周
console. log(date. getHours()); // 获取小时
console. log(date. getMinutes()); // 获取分钟
console. log(date. getSeconds()); // 获取秒
})()
}
</ script>


上面通过时间对象的各种方法获取需要的时间;然后根据自己的需要设计时间的显示方式即可;


下面显示一下 :2018/5/7  这种显示格式

< script>
window. onload = function(){
( function abc(){
var date =new Date();
var year =date. getFullYear();
var month =date. getMonth() + 1;
var day =date. getDate();
console. log(year + "/" +month + "/" +day);
})()
}
</ script>


下面显示一下:2018/05/07  这种显式格式

< script>
window. onload = function(){
( function abc(){
var date =new Date();

var year =date. getFullYear();
var month =date. getMonth() + 1;
var day =date. getDate();

month =month < 10 ? "0" +month :month;
day =day < 10 ? "0" +day :day;
console. log(year + "/" +month + "/" +day);
})()
}
</ script>


其它的各种格式基本可以根据上面的例子进行设计就可以了,按需构造;

猜你喜欢

转载自blog.csdn.net/cvper/article/details/80231752