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

JavaScript058

js 怎么获取年月日时分秒中的时分秒,第1张

需要准备的材料分别有:电脑、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获取并打印了出来。

可以用datetimepicker.js插件,我的bootstrap框架用bootstrap-datetimepicker.js,网上有datetimepicker.js插件。

在页面加载的时候初始化格式,如:

$('#id').datetimepicker({

    format: 'hh:ii:ss',

    autoclose: true,

    minView: 0,

    minuteStep:1})

如果有错误,请指出。

给你个思路:

1初始化时间,例如1小时5分钟30秒(也可以让用户手动设置,这里略)

保存在全局变量中

var hour,minute,second

2设置定时每隔1秒执行function xxx

setInterval(function xxx(){...},1000)

3编写function用于每隔1秒更新时间,里面判断若倒计时为0时,隐藏div

function xxx(){

if(--second==0){

if(--minute==0){

if(--hour==0){

//隐藏div 设置style.display='none'

}

show(hour,minute,second)

second=60

minute=60

}

show(hour,minute,second)

second=60

}

show(hour,minute,second)

}

function show(hour,minute,second){

var str_hour = hour<10?"0"+hour:""+hour

var str_minute = minute<10?"0"+minute:""+minute

var str_second = second<10?"0"+second:""+second

//将这三个时分秒显示到div中指定位置

}