js显示动态时间的方法

来源:互联网 发布:百度云总显示网络异常 编辑:程序博客网 时间:2024/06/01 11:42
js显示动态时间的方法

Date对象的方法

Date 对象能够使你获得相对于国际标准时间(格林威治标准时间,现在被称为 UTC-Universal Coordinated Time)或者是 Flash 播放器正运行的操作系统的时间和日期。要使用Date对象的方法,你就必须先创建一个Date对象的实体(Instance)。

Date 对象必须使用 Flash 5 或以后版本的播放器。

Date 对象的方法并不是静态的,但是在使用时却可以应用于所指定的单独实体。

Date 对象的方法简介:

·getDate            |  根据本地时间获取当前日期(本月的几号)
·getDay             |  根据本地时间获取今天是星期几(0-Sunday,1-Monday...)
·getFullYear        |  根据本地时间获取当前年份(四位数字)
·getHours           |  根据本地时间获取当前小时数(24小时制,0-23)
·getMilliseconds    |  根据本地时间获取当前毫秒数
·getMinutes         |  根据本地时间获取当前分钟数
·getMonth           |  根据本地时间获取当前月份(注意从0开始:0-Jan,1-Feb...)
·getSeconds         |  根据本地时间获取当前秒数
·getTime            |  获取UTC格式的从1970.1.1 0:00以来的毫秒数
·getTimezoneOffset  |  获取当前时间和UTC格式的偏移值(以分钟为单位)
·getUTCDate         |  获取UTC格式的当前日期(本月的几号)
·getUTCDay          |  获取UTC格式的今天是星期几(0-Sunday,1-Monday...)
·getUTCFullYear     |  获取UTC格式的当前年份(四位数字)
·getUTCHours        |  获取UTC格式的当前小时数(24小时制,0-23)
·getUTCMilliseconds |  获取UTC格式的当前毫秒数
·getUTCMinutes      |  获取UTC格式的当前分钟数
·getUTCMonth        |  获取UTC格式的当前月份(注意从0开始:0-Jan,1-Feb...)
·getUTCSeconds      |  获取UTC格式的当前秒数
·getYear            |  根据本地时间获取当前缩写年份(当前年份减去1900)
·setDate            |  设置当前日期(本月的几号)
·setFullYear        |  设置当前年份(四位数字)
·setHours           |  设置当前小时数(24小时制,0-23)
·setMilliseconds    |  设置当前毫秒数
·setMinutes         |  设置当前分钟数
·setMonth           |  设置当前月份(注意从0开始:0-Jan,1-Feb...)
·setSeconds         |  设置当前秒数
·setTime            |  设置UTC格式的从1970.1.1 0:00以来的毫秒数
·setUTCDate         |  设置UTC格式的当前日期(本月的几号)
·setUTCFullYear     |  设置UTC格式的当前年份(四位数字)
·setUTCHours        |  设置UTC格式的当前小时数(24小时制,0-23)
·setUTCMilliseconds |  设置UTC格式的当前毫秒数
·setUTCMinutes      |  设置UTC格式的当前分钟数
·setUTCMonth        |  设置UTC格式的当前月份(注意从0开始:0-Jan,1-Feb...)
·setUTCSeconds      |  设置UTC格式的当前秒数
·setYear            |  设置当前缩写年份(当前年份减去1900)
·toString           |  将日期时间值转换成"日期/时间"形式的字符串值
·Date.UTC           |  返回指定的UTC格式日期时间的固定时间值



页面js直接得到系统动态时间完整示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>JS实时显示时间</title></head><body><div id="localtime"></div><script type="text/javascript">function showLocale(objD){  var str,colorhead,colorfoot;  var yy = objD.getYear();   if(yy<1900) yy = yy+1900;  var MM = objD.getMonth()+1;   if(MM<10) MM = '0' + MM;  var dd = objD.getDate();   if(dd<10) dd = '0' + dd;  var hh = objD.getHours();   if(hh<10) hh = '0' + hh;  var mm = objD.getMinutes();   if(mm<10) mm = '0' + mm;  var ss = objD.getSeconds();   if(ss<10) ss = '0' + ss;  var ww = objD.getDay();   if ( ww==0 ) colorhead="<font color=\"#FF0000\">";   if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";   if ( ww==6 ) colorhead="<font color=\"#008000\">";   if (ww==0) ww="星期日";   if (ww==1) ww="星期一";   if (ww==2) ww="星期二";   if (ww==3) ww="星期三";   if (ww==4) ww="星期四";   if (ww==5) ww="星期五";   if (ww==6) ww="星期六";   colorfoot="</font>"    str = colorhead + yy + "年" + MM + "月" + dd + "日" + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;   //alert(str);return str;}function tick(){  var today;  today = new Date();  document.getElementById("localtime").innerHTML = showLocale(today);  window.setTimeout("tick()", 1000);}tick();</script></body></html>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head><title>HTML5 Video Destruction</title> <meta http-equiv="content-type" content="text/html; charset=gb2312"></head><body style="margin:0px;">  <div class="time">    <script type="text/javascript">        var hello;        hour = new Date().getHours()  //获取当前时间 (小时)        if (hour < 6) hello = '  凌晨好! '        else if (hour < 9) hello = ' 早上好! '        else if (hour < 12) hello = ' 上午好! '        else if (hour < 14) hello = ' 中午好! '        else if (hour < 17) hello = ' 下午好! '        else if (hour < 19) hello = ' 傍晚好! '        else if (hour < 22) hello = ' 晚上好! '        else {            hello = '夜深了! '        }        document.write('亲爱的用户:' + hello);//打印显示    </script>    <script type="text/javascript">       today = new Date();       var tdate, tday, x, year;       var x = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");       var MSIE = navigator.userAgent.indexOf("MSIE");       if (MSIE != -1) year = (today.getFullYear());       else year = (today.getYear() + 1900);       tdate = year + "年" + (today.getMonth() + 1) + "月" + today.getDate() + "日" + " " + x[today.getDay()];       document.write(tdate);    </script></div><div id="localtime"></div><script type="text/javascript">function showLocale(objD){var hello;        hour = new Date().getHours()  //获取当前时间 (小时)        if (hour < 6) hello = '  凌晨好! '        else if (hour < 9) hello = ' 早上好! '        else if (hour < 12) hello = ' 上午好! '        else if (hour < 14) hello = ' 中午好! '        else if (hour < 17) hello = ' 下午好! '        else if (hour < 19) hello = ' 傍晚好! '        else if (hour < 22) hello = ' 晚上好! '        else {            hello = '夜深了! '        }  var str,colorhead,colorfoot;  var yy = objD.getYear();   if(yy<1900) yy = yy+1900;  var MM = objD.getMonth()+1;   if(MM<10) MM = '0' + MM;  var dd = objD.getDate();   if(dd<10) dd = '0' + dd;  var hh = objD.getHours();   if(hh<10) hh = '0' + hh;  var mm = objD.getMinutes();   if(mm<10) mm = '0' + mm;  var ss = objD.getSeconds();   if(ss<10) ss = '0' + ss;  var ww = objD.getDay();   if ( ww==0 ) colorhead="<font color=\"#FF0000\">";   if ( ww > 0 && ww < 6 ) colorhead="<font color=\"#373737\">";   if ( ww==6 ) colorhead="<font color=\"#008000\">";   if (ww==0) ww="星期日";   if (ww==1) ww="星期一";   if (ww==2) ww="星期二";   if (ww==3) ww="星期三";   if (ww==4) ww="星期四";   if (ww==5) ww="星期五";   if (ww==6) ww="星期六";   colorfoot="</font>"       str =hello + colorhead + yy + "年" + MM + "月" + dd + "日" + hh + ":" + mm + ":" + ss + " " + ww + colorfoot;   //alert(str);return str;}function tick(){  var today;  today = new Date();  document.getElementById("localtime").innerHTML = showLocale(today);  window.setTimeout("tick()", 1000);}tick();</script></body></html>


原创粉丝点击