var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(),//hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length))
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length))
}
}
return format
}
new Date().format("hh:mm")//就是你想要的结果
1、新建一个HTML文件,命名为test.html。
2、在JS中使用new Date()获得当前系统的时间,并将其保存在变量d中。当前时间的年份使用getYear()方法从变量d中获得。
3、使用new Date()获得当前系统的时间,并将其保存在变量d中。当前时间的月份使用getMonth()方法从变量d中获得。
4、在JS中使用new Date()获得当前系统的时间,并将其保存在变量d中。当前时间的日期使用getDate()方法从变量d中获得。
5、在JS中使用new Date()获得当前系统的时间,并将其保存在变量d中。当前时间的小时使用getHours()方法从变量d中获得。
6、在JS中使用new Date()获得当前系统的时间,并将其保存在变量d中。当前时间的秒数使用getSeconds()方法从变量d中获得。
7、就完成了。
希望可以帮到你!//1.前面补0
function p(n){
return n<10?'0'+n:n
}
//2.倒计时
function getMyTime(){
var startDate=new Date()
var endDate=new Date('2017/4/17 11:15:00')
var countDown=(endDate.getTime()-startDate.getTime())/1000
var day=parseInt(countDown/(24*60*60))
var h=parseInt(countDown/(60*60)%24)
var m=parseInt(countDown/60%60)
var s=parseInt(countDown%60)
document.getElementById('time').innerHTML=day+'天'+p(h)+'时'+p(m)+'分'+p(s)+'秒'
setTimeout('getMyTime()',500)
if(countDown<=0){
document.getElementById('time').innerHTML='活动结束'
}
}