JS怎么修改时间显示的格式。感谢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS怎么修改时间显示的格式。感谢相关的知识,希望对你有一定的参考价值。
例如上图: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>
自定义日期格式化函数
格式化函数
// 对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 time2 = new Date().format("yyyy-MM-dd"); 参考技术A
样例代码:
<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>
/// <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"));
JS怎么判断8点30分上班,到11点30分就显示下班
javascript怎么写,能判断系统时间8点30分的时候就显示上班,带11点30分的时候就显示下班。求大侠高手们帮忙!!!感谢感谢!!
参考技术A <html><head>
<title>JS怎么判断8点30分上班,到11点30分就显示下班</title>
</head>
<script type="text/javascript">
function popup()
var t = new Date().toLocaleTimeString();
if (t == "08:30:00" || t == "8:30:00")
alert("上班");
else if (t == "11:30:00")
alert("下班");
setInterval(popup,100);
</script>
<body>
</body>
</html> 参考技术B <html>
<head>
<title>Picture Scroll</title>
</head>
<body>
<div id="time"></div>
<script type="text/javascript">
window.onload = date();
function date()
var date = new Date();
time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
document.getElementById("time").innerHTML=time;
setTimeout("date()",1000);
if(date.getHours()==8 && date.getMinutes()==30)
alert("Time to work");
if(date.getHours()==11 && date.getMinutes()==30)
alert("Time for lunch");
</script>
</body>
</html> 参考技术C <script type="text/javascript">
function panduanShijian()
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
if( hours == 8 && minutes == 30 )
alert('上班');
else if( hours == 11 && minutes == 30 )
alert('下班');
setTimeout("panduanShijian()", 60000);
panduanShijian();
</script>本回答被提问者采纳
以上是关于JS怎么修改时间显示的格式。感谢的主要内容,如果未能解决你的问题,请参考以下文章