JS怎么修改时间显示的格式。感谢
例如上图:6小时48分2秒,求帮忙修改为06小时48分02秒也就是双位数代码如下:<html><head><title>倒计时,每天00:00分重新倒计时。每天重复</t...
例如上图:6小时48分2秒,求帮忙修改为06小时48分02秒 也就是双位数
代码如下:
<html>
<head><title>倒计时,每天00:00分重新倒计时。每天重复</title><style type="text/css">#time {font-size: 24px;}</style></head><body><script language=JavaScript> var now = new Date();//当前时间 var isjx=0; function GetServerTime(){ var d= now.getYear()+"/"+now.getMonth()+"/"+now.getDate()+" 00:00:00";//设置每天的16:30 为节点 var urodz = new Date(d); now.setTime(now.getTime()+250); days = (urodz - now) / 1000 / 60 / 60 / 24; daysRound = Math.floor(days); hours = (urodz - now) / 1000 / 60 / 60 - (24 * daysRound); hoursRound = Math.floor(hours); minutes = (urodz - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); minutesRound = Math.floor(minutes); seconds = (urodz - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); secondsRound = Math.round(seconds); if((hoursRound==0 && minutesRound==0 && secondsRound==0)){//都等于0 说明过了16:30 isjx=1; } //判断 if(isjx==0 && (parseFloat(now.toTimeString().substr(0,2)+ now.toTimeString().substr(3,3).substr(0,2)+now.toTimeString().substr(6,7) )<=162959)){ document.getElementById("time").innerHTML = "距离开始 还有"+hoursRound + "小时" + minutesRound + "分钟" + secondsRound +"秒" ; }else { document.getElementById("time").innerHTML = "距离结束 还有 "+hoursRound + "小时" + minutesRound + "分钟" + secondsRound +"秒"; } } setInterval("GetServerTime()",250);</script> <span id="time"></span> </body></html> 展开
代码如下:
<html>
<head><title>倒计时,每天00:00分重新倒计时。每天重复</title><style type="text/css">#time {font-size: 24px;}</style></head><body><script language=JavaScript> var now = new Date();//当前时间 var isjx=0; function GetServerTime(){ var d= now.getYear()+"/"+now.getMonth()+"/"+now.getDate()+" 00:00:00";//设置每天的16:30 为节点 var urodz = new Date(d); now.setTime(now.getTime()+250); days = (urodz - now) / 1000 / 60 / 60 / 24; daysRound = Math.floor(days); hours = (urodz - now) / 1000 / 60 / 60 - (24 * daysRound); hoursRound = Math.floor(hours); minutes = (urodz - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); minutesRound = Math.floor(minutes); seconds = (urodz - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); secondsRound = Math.round(seconds); if((hoursRound==0 && minutesRound==0 && secondsRound==0)){//都等于0 说明过了16:30 isjx=1; } //判断 if(isjx==0 && (parseFloat(now.toTimeString().substr(0,2)+ now.toTimeString().substr(3,3).substr(0,2)+now.toTimeString().substr(6,7) )<=162959)){ document.getElementById("time").innerHTML = "距离开始 还有"+hoursRound + "小时" + minutesRound + "分钟" + secondsRound +"秒" ; }else { document.getElementById("time").innerHTML = "距离结束 还有 "+hoursRound + "小时" + minutesRound + "分钟" + secondsRound +"秒"; } } setInterval("GetServerTime()",250);</script> <span id="time"></span> </body></html> 展开
3个回答
展开全部
自定义日期格式化函数
格式化函数
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), 凯磨饥 //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); //格式化年份游虚
for(var k in o) //循环获取上面定义的月、日、小时等,格式化对应的数据。
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 盯返
return fmt;
}
使用示例:
var time1 = new Date().format("yyyy-MM-dd HH:mm:ss");
var time2 = new Date().format("yyyy-MM-dd");
展开全部
样例代码:
<html>
<head>
<title>知道</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
#time {font-size: 24px;}
</style>
</head>
<body>
<script language=JavaScript>
var formatZero = function(n){
return (n-9>0)?n:'0'+n;
}
var now = new Date();//当前时间
var isjx=0;
function GetServerTime(){
var d= now.getYear()+"/"+now.getMonth()+"/"+now.getDate()+" 00:00:00";//设置每天的16:30 为节点
var urodz = new Date(d);
now.setTime(now.getTime()+250);
days = (urodz - now) / 1000 / 60 / 60 / 24;
daysRound = Math.floor(days);
hours = (urodz - now) / 1000 / 60 / 60 - (24 * daysRound);
hoursRound = Math.floor(hours);
minutes = (urodz - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
minutesRound = Math.floor(minutes);
seconds = (urodz - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.round(seconds);
if((hoursRound==0 && minutesRound==0 && secondsRound==0)){//都等于0 说明过了16:30
isjx=1;
}
hoursRound = formatZero(hoursRound);
minutesRound = formatZero(minutesRound);
secondsRound = formatZero(secondsRound);
//判断
if(isjx==0 && (parseFloat(now.toTimeString().substr(0,2)+ now.toTimeString().substr(3,3).substr(0,2)+now.toTimeString().substr(6,7) )<=162959)){
document.getElementById("time").innerHTML = "距离开始 还有"+hoursRound + "小时" + minutesRound + "分钟" + secondsRound +"秒" ;
}else {
document.getElementById("time").innerHTML = "距离结束瞎侍 还有 "+hoursRound + "小时" + minutesRound + "分钟" + secondsRound +"秒";
}
}
汪罩setInterval("GetServerTime()",250);
</script>
<span id="time">磨陵吵</span>
</body>
</html>
希望对你有帮助。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/// <summary>
/// 格式化显示日期时间
/// </summary>
/// <param name="x">待显示的日期时间,例如new Date()</param>
/// <param name="y">需要显示的格式,例如yyyy-MM-dd hh:mm:ss</param>
function date2str(x,y) 改枝{
var 咐歼汪z = {M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()};
y = y.replace(/(M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-2)});
return y.replace(/(y+)/g,function(v) {return x.getFullYear().toString().slice(-v.length)});
} 衡仔
alert(date2str(new Date(),"yyyy-MM-dd hh:mm:ss"));
alert(date2str(new Date(),"yyyy-M-d h:m:s"));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询