本文主要是介绍两例JavaScript 获取当前系统日期和时间,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
两例JavaScript 获取当前系统日期和时间
QUOTE:
<script language="JavaScript">
//定义一个tick函数,以获取系统的时间
function tick()
{
var year,month,day,hours,minutes,seconds,ap;
var intYear,intMonth,intDay,intHours,intMinutes,intSeconds;
var today;
today=new Date();
intYear=today.getYear();
intMonth=today.getMonth()+1;
intDay=today.getDate();
intHours=today.getHours();
intMinutes=today.getMinutes();
intSeconds=today.getSeconds();
//获取系统时间的小时数
if(intHours==0)
{
hours=intHours+":";
ap="凌晨";
}
else if(intHours<12)
{
hours=intHours+":";
ap="早晨";
}
else if(intHours==12)
{
hours=intHours+":";
ap="中午";
}
else
{
intHours=intHours-12;
hours=intHours+":";
ap="下午";
}
//获取系统时间的分数
if(intMinutes<10)
{
minutes="0"+intMinutes+":";
}
else
minutes=intMinutes+":";
//获取系统时间的秒数
if(intSeconds<10)
seconds="0"+intSeconds+" ";
else
seconds=intSeconds+" ";
timeString=year+month+day+hours+minutes+seconds+ap;
Clock.innerHTML=timeString;
//每隔0.1秒钟执行一次tick函数
window.setTimeout("tick()",100);
}
window.οnlοad=tick;
</script>
下面这句是调用函数
QUOTE:
<div id="Clock"></div>
----------------------------------------可爱的分割线--------------------------------------------------
QUOTE:
<SCRIPT language=JavaScript>
var day="";
var month="";
var ampm="";
var ampmhour="";
var myweekday="";
var year="";
mydate=new Date();
myweekday=mydate.getDay();
mymonth=mydate.getMonth()+1;
myday= mydate.getDate();
myyear= mydate.getYear();
year=(myyear > 200) ? myyear : 1900 + myyear;
if(myweekday == 0)
weekday=" 星期日 ";
else if(myweekday == 1)
weekday=" 星期一 ";
else if(myweekday == 2)
weekday=" 星期二 ";
else if(myweekday == 3)
weekday=" 星期三 ";
else if(myweekday == 4)
weekday=" 星期四 ";
else if(myweekday == 5)
weekday=" 星期五 ";
else if(myweekday == 6)
weekday=" 星期六 ";
document.write("<font color=#ff0000>北京时间 "+year+"年"+mymonth+"月"+myday+"日 "+weekday+"</font>");
</SCRIPT>
--------------------------------------------------------------------------------------------------------------
- <span id="curTime"></span>
<script type="text/javascript">
setInterval("curTime.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
</script>
这一句js 就可以完成 你的功能了
这篇关于两例JavaScript 获取当前系统日期和时间的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!