javascript 程序 日期星期的显示

来源:百度文库 编辑:神马文学网 时间:2024/05/02 08:29:14
日期星期的显示
源程序代码:

源程序讲解:
todayDate = new Date();
当定义一个新的对象时,通常使用“new”操作符。在这里,就是创建了日期对象。
date = todayDate.getDate();
getDate()是Date对象的一种方法,其功能是获得当前的日期。
month= todayDate.getMonth() + 1 ;
;
getMonth()是Date对象的一种方法,其功能是获得当前的日期,由于月份是从0开始的,所以这里要“+1”。
year= todayDate.getYear()
getYear()是Date对象的一种方法,其功能是获得当前的年份。
document.write("今天是") document.write("
")
输出“今天是”
if(navigator.appName == "Netscape") { document.write(1900+year); document.write("年"); document.write(month); document.write("月"); document.write(date); document.write("日");
document.write("
") }
如果浏览器是Netscape,输出今天是“year”+“年”+“month”+“月”+“date”+“日”,其中年要加1900。
if(navigator.appVersion.indexOf("MSIE") != -1) { document.write(year); document.write("年"); document.write(month); document.write("月"); document.write(date); document.write("日");
document.write("
") }
如果浏览器是IE,直接输出今天是“year”+“年”+“month”+“月”+“date”+“日”。
document.write("") ;
在“日期”与“星期”之间输入一个空格。
if (todayDate.getDay() == 5) document.write("星期五");
if (todayDate.getDay() == 6) document.write("星期六");
if (todayDate.getDay() == 0) document.write("星期日");
if (todayDate.getDay() == 1) document.write("星期一");
if (todayDate.getDay() == 2) document.write("星期二");
if (todayDate.getDay() == 3) document.write("星期三");
if (todayDate.getDay() == 4) document.write("星期四")
getDay()是Date对象的一种方法,其功能是获得当前是星期几。document.write输出今天是“星期几”。