JavaScript获取系统当前日期和时间、星期几
方法一:
<script language="javascript">
var mydate=new Date();
var myyear=mydate.getYear();
var mymonth=mydate.getMonth()+1;//注:月数从0~11为一月到十二月
var mydat=mydate.getDate();
var myhours=mydate.getHours();
var myminutes=mydate.getMinutes();
var myseconds=mydate.getSeconds()
var myday=mydate.getDay()//注:0-6对应为星期日到星期六
var xingqi
switch(myday)
{
case 0:xingqi="星期日";break;
case 1:xingqi="星期一";break;
case 2:xingqi="星期二";break;
case 3:xingqi="星期三";break;
case 4:xingqi="星期四";break;
case 5:xingqi="星期五";break;
case 6:xingqi="星期六";break;
default:xingqi="系统错误!"
}
document.write("现在的时间是:")
document.write("<br>")
document.write(""+myyear+"年"+mymonth+"月"+mydat+"日");
document.write("<br>")
document.write(""+myhours+"时"+myminutes+"分"+myseconds+"秒");
document.write("<br>")
document.write(xingqi)
</script>
------------------------------------------------------------------
方法二:
(自定义函数法,然后调用)
<script language="javascript">
function xingqi()
{
var a=new Date()
var myxingqi=a.getDay()
var b
switch(myxingqi)
{
case 0:b="星期日";break;
case 1:b="星期一";break;
case 2:b="星期二";break;
case 3:b="星期三";break;
case 4:b="星期四";break;
case 5:b="星期五";break;
case 6:b="星期六";break;
default:b="系统错误,无法读取日期!";
}
document.write(b)
}
xingqi()
</script>
----------------------------------------------------------------
方法三:(数组方法)
<script language="javascript">
var a=new Date()
var myxingqi=a.getDay()
var b=new Array()
b[0]="星期日";
b[1]="星期一";
b[2]="星期二";
b[3]="星期三";
b[4]="星期四";
b[5]="星期五";
b[6]="星期六";
document.write(b)
</script>
或者
<script language="javascript">
var a=new Date()
var myxingqi=a.getDay()
var b=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六")
document.write(b)
</script>
方法一:
<script language="javascript">
var mydate=new Date();
var myyear=mydate.getYear();
var mymonth=mydate.getMonth()+1;//注:月数从0~11为一月到十二月
var mydat=mydate.getDate();
var myhours=mydate.getHours();
var myminutes=mydate.getMinutes();
var myseconds=mydate.getSeconds()
var myday=mydate.getDay()//注:0-6对应为星期日到星期六
var xingqi
switch(myday)
{
case 0:xingqi="星期日";break;
case 1:xingqi="星期一";break;
case 2:xingqi="星期二";break;
case 3:xingqi="星期三";break;
case 4:xingqi="星期四";break;
case 5:xingqi="星期五";break;
case 6:xingqi="星期六";break;
default:xingqi="系统错误!"
}
document.write("现在的时间是:")
document.write("<br>")
document.write(""+myyear+"年"+mymonth+"月"+mydat+"日");
document.write("<br>")
document.write(""+myhours+"时"+myminutes+"分"+myseconds+"秒");
document.write("<br>")
document.write(xingqi)
</script>
------------------------------------------------------------------
方法二:
(自定义函数法,然后调用)
<script language="javascript">
function xingqi()
{
var a=new Date()
var myxingqi=a.getDay()
var b
switch(myxingqi)
{
case 0:b="星期日";break;
case 1:b="星期一";break;
case 2:b="星期二";break;
case 3:b="星期三";break;
case 4:b="星期四";break;
case 5:b="星期五";break;
case 6:b="星期六";break;
default:b="系统错误,无法读取日期!";
}
document.write(b)
}
xingqi()
</script>
----------------------------------------------------------------
方法三:(数组方法)
<script language="javascript">
var a=new Date()
var myxingqi=a.getDay()
var b=new Array()
b[0]="星期日";
b[1]="星期一";
b[2]="星期二";
b[3]="星期三";
b[4]="星期四";
b[5]="星期五";
b[6]="星期六";
document.write(b)
</script>
或者
<script language="javascript">
var a=new Date()
var myxingqi=a.getDay()
var b=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六")
document.write(b)
</script>