代码如下:
<!DOCTYPE html> ?<html> ????<head> ?????????<meta charset="utf-8" /><title></title> ????</head> ????<body> ??????????<div style="background-color:yellow;"> ????????????<span> ??????????????????<script type="text/javascript"> ????????????????????var date = new Date(); ????????????????????document.write("今天是:" + date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日" + " 星期" + "日一二三四五六".charAt(date.getDay())); ??????????????????</script> ????????????</span> ????????</div> ???</body> </html>
结果显示为
var date = new Date(); ?声明一个名称为date的对象,赋值为new Date(); new Date();表示创建了一个日期对象,返回值为当前日期。
document.write(); ??????向输出流(文档)写入文本或HTML。
date.getFullYear() ?????从Date对象以四位数字返回年份
date.getMonth()+1 ??????从Date对象返回月份(0-11),返回当前月份需+1。 ?
date.getDate() ?????????从Date对象返回一个月中的某一天(1-31)。
stringObject.charAt(index) ??????????方法可返回指定位置的字符。index表示字符串中某个位置的数字,即字符在字符串中的下标
date.getDay() ??????????从 Date 对象返回一周中的某一天 (0 ~ 6)。0为星期日。
"日一二三四五六".charAt(date.getDay()) ?就是date.getDay()返回0~6数字,同时代表"日一二三四五六"字符串的下标。
JS 在HTML页面显示当前日期
原文地址:https://www.cnblogs.com/guozhihua/p/10307630.html