javascript 分别读取时间中的年、月、日、时、分、秒的方法
因为网页的需要,希望用javascript 读取时间,但是需要将年、月、日、时、分、秒以不同的字体、大小等显示,所以希望能将年、月、日、时、分、秒分开进行读取。
打个比方,一般网上的时间代码可以显示为:XXXX年XX月XX日 XX:XX:XX
但是我希望这样显示:
XXXX年(白色,Size=5)
XX月XX日(蓝色,Size=3)
XX时(白色,Size=5)
XX分XX秒(白色,Size=3)
也就是说年、月、日、时、分、秒每个数据的显示都可以分开来。
不知道大家听懂了没,麻烦大家了。
XXXX年(白色,Size=5)
XX月XX日(蓝色,Size=3)
XX时(白色,Size=5)
XX分XX秒(白色,Size=3)
括号中的内容是网页代码的意思啊 。
我希望是这样的 :
<font color="#FFFFFF" size="5">XXXX年</font>
<font color="blue" size="3">XX月XX日</font>
<font color="#FFFFFF" size="5">XX时</font>
<font color="#FFFFFF" size="5">XX分XX秒</font>
意思就是每一个数据我都可以设置字体的颜色、大小等 ,另外 ,因为我基本上对javascript 一点也不懂,所以麻烦大家给完整代码吧 就是直接放到网页就可以显示的那种 ,前面后面有 <script type="text/javascript">和</script>这种格式的 。
还有就是 XX秒 和 XX分 XX时 这几个能弄动态的么 。
javascript 自带有亩答逗个对象(构造举数函数),Date().下面是代码:
var myDate = new Date(); //实例一个时间对象;
myDate.getFullYear(); //获取系统的年;
myDate.getMonth()+1; //获取系统月份,由于月份是从0开始计算,所以要加1
myDate.getDate(); // 获取系统日,
myDate.getHours(); //获取系统迅卖时,
myDate.getMinutes(); //分
myDate.getSeconds(); //秒
getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)
getMonth() 从 Date 对象返回月份 (0 ~ 11)//注意月份是从0开始,使用的时候要+1
getFullYear() 从 Date 对象以四位数掘纳字返回年份
getHours() 返回 Date 对象的小时 (0 ~ 23)
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)
getSeconds() 返回 Date 对象的秒数 (0 ~ 59))
getMilliseconds() 返回 Date 对象判拦没衡宴的毫秒(0 ~ 999)
例如:
<script>
var d = new Date()
document.write("Year:"+d.getYear()+"<br>Month:"+(d.getMonth()+1)+"<br>Day:"+d.getDate()+"<br>Hour:"+d.getHours()+"<br>Minutes"+d.getMinutes()+"<br>Seconds:"+d.getSeconds())
</script>
get[UTC]Date( )
Returns the day of the month, in local or universal time. Return values are between 1 and 31.
get[UTC]Day( )
Returns the day of the week, in local or universal time. Return values are between 0 (Sunday) and 6 (Saturday).
get[UTC]FullYear( )
Returns the year in full four-digit form, in local or universal time. JS 1.2; JScript 3.0; ECMA v1.
get[UTC]Hours( )
Returns the hours field, in local or universal time. Return values are between 0 (midnight) and 23 (11 p.m.).
get[UTC]Milliseconds( )
Returns the milliseconds field, in local or universal time. JS 1.2; JScript 3.0; ECMA v1.
get[UTC]Minutes( )
Returns the minutes field, in local or universal time. Return values are between 0 and 59.
get[UTC]Month( )
Returns the month field, in local or universal time. Return values are between 0 (January) and 11 (December).
get[UTC]Seconds( )
Returns the seconds field, in local or universal time. Return values are between 0 and 59.
getTime( )
Returns the internal millisecond representation of the date; that is, returns the number of milliseconds between midnight (UTC) of January 1st, 1970 and the date and time represented by the Date object. Note that this value is independent of timezone.
getTimezoneOffset( )
Returns the difference, in minutes, between the local and UTC representations of this date. Note that the value returned depends on whether daylight savings time is or would be in effect at the specified date.
getYear( )
Returns the year field minus 1900. Deprecated in favor of getFullYear( ).
<!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=gb2312" />
<title>无标题文档圆笑</title>
<script language="javascript" type="text/javascript">
<!--
//获得当前时间,刻度为一千分一秒
var initializationTime=(new Date()).getTime();
function showLeftTime()
{
var now=new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
document.all.show.innerHTML="当前时间:<font size='+5' color='#FFFFF'>"+year+"年</font><font size='+3' color='#000033'>"+month+"月"+day+"日</font> <font size='+5' color='#FFFFF'>"+hours+"</font>:<font size='+3' color='#000033'>"+minutes+":"+seconds+"</font>";
//一秒刷新一次显示时旦绝间
var timeID=setTimeout(showLeftTime,1000);
}
//-->
</script>
</head>
<body onload="showLeftTime()">
<div style="background-color:#66FF00"模腔姿>
<label id="show"></label><br>
</div>
</body>
</html>
是不是这个意思啊?
如果不对,请HI我
function abc()
{
var now = new Date(2010,7,8,23,59,59);
var yy = now.getYear();
var mm = now.getMonth();
var dd = now.getDate();
var hh = now.getHours();
var mi = now.getMinutes();
var ss = now.getSeconds();
alert(yy);
alert(mm);
alert(dd);
alert(hh);
alert(mi);
alert(ss);
}