js 时间获取

JavaScript日期格式化

先看看如何获取时间:

<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>


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值