js 怎么获取年月日时分秒中的时分秒

如题所述

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:

var a = new Date();document.body.innerHTML 

= '时:' + a.getHours() + '<br/>分:' + a.getMinutes() + '<br/>秒:' + a.getSeconds();

3、浏览器运行index.html页面,此时当前时间的时分秒都被js获取并打印了出来。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-03-09
var date = new Date();//实例一个时间对象;
var year = date.getFullYear();//获取系统的年;
var month = date.getMonth()+1;//获取系统月份,由于月份是从0开始计算,所以要加1
var day = date.getDate();获取系统日
var hour = date.getHours();//获取系统时间
var minute = date.getMinutes(); //分
var second = date.getSeconds();//秒
alert(year+'年'+month+'月'+day+'日 '+hour':'+minute+':'+second)
第2个回答  2017-03-09
<script type="text/javascript">
var tody = new Date();
var nian = tody.getFullYear();
var youe = tody.getMonth() + 1;
var day = tody.getDate();
var hour = tody.getHours();
var min = tody.getMinutes();
var miao = tody.getSeconds();
console.log(tody)
console.log(nian)
console.log(youe)
console.log(day)
console.log(hour)
console.log(min)
console.log(miao)
</script>本回答被提问者采纳