date对象

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>date对象</title>
<script type="text/javascript">
//定义函数writeIn(),输出内容
function writeIn(mydate){
document.write(mydate+"<br />");
}

//创建date对象
var dates = new Date();

//获取年份
writeIn("获取年份:"+dates.getFullYear());

//获取月份
writeIn("获取月份:"+(dates.getMonth()+1));

//获取日期
writeIn("获取日期:"+dates.getDate());

//获取小时
writeIn("获取小时:"+dates.getHours());

//获取分钟
writeIn("获取分钟:"+dates.getMinutes());

//获取秒钟
writeIn("获取秒钟:"+dates.getSeconds());

//获取星期
writeIn("获取星期:"+dates.getDay());
switch(dates.getDay()){
case 0:document.write("星期日"+"<br />");break;
case 1:document.write("星期一"+"<br />");break;
case 2:document.write("星期二"+"<br />");break;
case 3:document.write("星期三"+"<br />");break;
case 4:document.write("星期四"+"<br />");break;
case 5:document.write("星期五"+"<br />");break;
case 6:document.write("星期六"+"<br />");break;
default:
document.write("错误");
}

//返回 1970 年 1 月 1 日至今的毫秒数。
writeIn("返回 1970 年 1 月 1 日至今的毫秒数:"+dates.getTime());

// 返回本地时间与格林威治标准时间 (GMT) 的分钟差。
writeIn("本地时间与格林威治标准时间 (GMT) 的分钟差:"+dates.getTimezoneOffset());

//根据世界时(UTC)从 Date 对象返回四位数的年份。与本地时间一样就方法中间差了一个UTC
writeIn("根据世界时从 Date 对象返回四位数的年份:"+dates.getUTCFullYear());

//设置 Date 对象中月份 (0 ~ 11)。  setFullYear()  setMonth()  setDate()  setHours()  setMinutes()  setSeconds()
writeIn("设置 Date 对象中月份 (0 ~ 11):"+dates.setMonth(5));   //返回值:调整过的日期以毫秒表示
writeIn(dates);

//把 Date 对象转换为字符串。
writeIn("把 Date 对象转换为字符串:"+dates.toString());

//返回 Date 对象的原始值。
writeIn("返回 Date 对象的原始值:"+dates.valueOf());


</script>
</head>
<body>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/grass_root_boy/article/details/79878306