<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js Date方法</title>
</head>
<body>
<script>
/*参考:https://www.w3school.com.cn/js/jsref_obj_date.asp*/
// 1.Date() 返回当前的日期和时间。
let myDate = new Date();
console.log("myDate:", myDate);
// myDate: Tue Aug 06 2019 09:02:46 GMT+0800 (中国标准时间)
console.log(typeof myDate); // object
// 2.dateObj.getFullYear() 从 Date 对象以四位数字返回年份。
console.log("fullYear:", myDate.getFullYear());
// fullYear: 2019
// 3.dateObj.getMonth() 从 Date 对象返回月份 (0 ~ 11)。
// 0,代表1月;11,代表12月。
console.log("month:", myDate.getMonth() + 1);
// month: 8
// 4.dateObj.getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
console.log("date:", myDate.getDate());
// date: 6
// 5.dateObj.getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
// 0,代表星期日;1-6,代表星期一-星期六。
let day;
switch (myDate.getDay()) {
case 0:
day = "星期日";
break;
case 1:
day = "星期一";
break;
case 2:
day = "星期二";
break;
case 3:
day = "星期三";
break;
case 4:
day = "星期四";
break;
case 5:
day = "星期五";
break;
case 6:
day = "星期六";
}
console.log("day:", day);
// day: 星期二
// 6.dateObj.getHours(),返回 Date 对象的小时 (0 ~ 23)。
// 其中,0,代表24时。
console.log("hours:", myDate.getHours());
// hours: 9
// 7.dateObj.getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
console.log("minutes:", myDate.getMinutes());
// minutes: 2
// 8.dateObj.getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
console.log("seconds:", myDate.getSeconds());
// seconds: 46
// 9.dateObj.getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
console.log("milliseconds:", myDate.getMilliseconds());
// milliseconds: 924
// 10.dateObj.getTime() 返回 1970 年 1 月 1 日00:00:00至今的毫秒数。
// new Date().getTime() 和 Date.now()、Number(new Date()) 结果一致。
console.log("\n10.");
console.log("milliseconds:", myDate.getTime());
// milliseconds: 1589636294953
console.log("new Date().getTime():", new Date().getTime());
// new Date().getTime(): 1589636294956
console.log("Date.now():", Date.now());
// Date.now(): 1589636294957
console.log("Number(Date()):", Number(new Date()));
// Number(Date()): 1589636294957
// 11.dateObj.setTime() 以毫秒设置 Date 对象。
// myDate.setTime(152422609553);
// console.log("myDate:", myDate);
// myDate: Thu Oct 31 1974 11:36:49 GMT+0800 (中国标准时间)
// 12.dateObj.toUTCString()
// 根据世界时 (UTC) 把 Date 对象转换为字符串,并返回结果。
// UTC时间加 8 小时,就是中国时间。
console.log("\n12.");
console.log("UTCStr:", myDate.toUTCString());
// UTCStr: Sun, 17 May 2020 05:03:53 GMT
// 13.dateObj.toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。
console.log("\n13.");
console.log("localeString:", myDate.toLocaleString());
// localeString: 2020/5/17 下午1:03:53
// 14.dateObj.toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
console.log("\n14.");
console.log("localeDateString:", myDate.toLocaleDateString());
// localeDateString: 2020/5/17
console.log(typeof myDate.toLocaleDateString());
// string
// 15.dateObj.toLocaleTimeString()
// 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
// 返回当前的本地时间字符串(时分秒),而不是格林尼治时间.
console.log("\n15.");
console.log("localeTimeString:", myDate.toLocaleTimeString());
// localeTimeString: 下午1:03:53
// 16.dateObj.toString() 把 Date 对象转换为字符串。
console.log("dateStr:", myDate.toString());
// dateStr: Tue Aug 06 2019 09:02:46 GMT+0800 (中国标准时间)
console.log(typeof myDate.toString());
// string
// 17.dateObj.toDateString() 把 Date 对象的日期部分转换为字符串。
console.log("dateString:", myDate.toDateString());
// dateString: Tue Aug 06 2019
// 18.dateObj.toTimeString()
console.log("timeString:", myDate.toTimeString());
// timeString: 09:02:46 GMT+0800 (中国标准时间)
// 19.dateObj.setDate()
// 注意: 如果增加天数会改变月份或者年份,那么日期对象会自动完成这种转换。
myDate.setDate(myDate.getDate() + 5);
console.log("myDate:", myDate);
// myDate: Sun Apr 12 2020 12:13:13 GMT+0800 (中国标准时间)
// 20.dateObj.setFullYear(year,month?,day?) -> 调整过的日期的毫秒表示。
// 功能:设置年月日。
// year,必需。表示年份的四位整数。用本地时间表示。
// month,可选。表示月份的数值,介于 0 ~ 11 之间。用本地时间表示。
// day,可选。表示月中某一天的数值,介于 1 ~ 31 之间。用本地时间表示。
// 注:时分秒,仍是当前时间的时分秒。
myDate.setFullYear(2020, 10, 3);
console.log("myDate:", myDate);
// myDate: Tue Nov 03 2020 12:22:38 GMT+0800 (中国标准时间)
</script>
</body>
</html>
02-js_Date()方法.html
于 2019-04-03 09:58:40 首次发布