Date对象(日期对象)
一、概念
Date对象 用于处理日期和时间。
二、创建 Date 对象
可以通过 new 关键词来定义 Date 对象。
注:Date 对象会自动把当前日期和时间保存为其初始值。
//四种方式初始化日期:【接受不同参数】
new Date() // 当前日期和时间
new Date(milliseconds) //返回从 1970 年 1 月 1 日至今的毫秒数
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
//参数:上面的参数大多数都是可选的,默认为0。
(一)默认参数→0
new Date()
当前日期和时间
console.log(new Date());//Thu Dec 06 2018 15:11:13 GMT+0800 (中国标准时间)
(二)参数→毫秒数
new Date(milliseconds)
参数(milliseconds):从1970年1月1日00:00:00 UTC开始计算的毫秒数。如果将Unix时间戳作为参数,必须将Unix时间戳乘以1000。【unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒】
(三)参数→日期字符串
new Date(dateString)
参数(dateString):一个日期字符串,返回所对应的时间。所有可以被Date.parse()方法解析的日期字符串,都可以当作Date对象的参数
(四)参数→年、月、日、小时、分钟、秒和毫秒
new Date(year, month, day, hours, minutes, seconds, milliseconds)
参数(year, month, day, hours, minutes, seconds, milliseconds):对应的年、月、日、小时、分钟、秒和毫秒。
注:
- 至少需要指定两个参数(年和月),其他参数都是可选的,默认为0。
- 若只有一个参数,Date对象会将其解释为毫秒数。
console.log(new Date(2018)); //Thu Jan 01 1970 08:00:02 GMT+0800 (中国标准时间)
console.log(new Date(2018,12)); //Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2018,12,6)); //Sun Jan 06 2019 00:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2018,12,6,17)); //Sun Jan 06 2019 17:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2018,12,6,17,2)); //Sun Jan 06 2019 17:02:00 GMT+0800 (中国标准时间)
console.log(new Date(2018,12,6,17,2,36)); //Sun Jan 06 2019 17:02:36 GMT+0800 (中国标准时间)
console.log(new Date(2018,12,6,17,2,36,267)); //Sun Jan 06 2019 17:02:36 GMT+0800 (中国标准时间)
三、时间的表现方式
GMT
GMT(Greenwich Mean Time):格林威治时间
UTC
UTC(Coordinated Universal Time):世界协调时间
CST
CST(Computer Simulation Technology):中部标准时间。可视为美国、澳大利亚、古巴或中国的标准时间。
EST
CST(Computer Simulation Technology):东部标准时间。
……
四、常用方法
(一)get
getTime(): 返回实例对象当前时间距离1970年1月1日00:00:00对应的毫秒数
getFullYear(): 返回调用这个方法时,当前或指定日期的年份(四位年份)
getMonth(): 返回月份,(0表示1月,11表示12月)调用这个方法时,当前或指定日期的月份
getDate(): 返回日(号),调用这个方法时,当前或指定日期的月份中的几号
getDay(): 返回星期,周日为0,周一为1,以此类推(0-6)
getHours(): 返回小时(0-23)
getMinutes(): 返回分钟(0-59)
getSeconds(): 返回秒(0-59)
getMilliseconds(): 返回毫秒(0-999)
var date1 = new Date();//当前日期:Thu Dec 06 2018 11:05:07 GMT+0800 (中国标准时间)
var date = new Date("2018-12-06 11:05:45");
console.log(date1.getTime());//1544065812928,当前时间差毫秒数,刷新会变
console.log(date.getTime());//1544065545000,指定时间距时间差毫秒数,刷新不会变
console.log(date1.getFullYear());//2018
console.log(date.getFullYear());//2018
console.log(date1.getMonth());//11
console.log(date.getMonth());//11
console.log(date.getDate());//6
console.log(date1.getDay());//4
console.log(date.getDay());//4
(二)set
setTime(): 以毫秒设置 Date 对象。
setFullYear(): 设置年份。
setMonth(month,day): 设置月份。可用于设置月份中的某一天。
setDate(): 设置一个月的某一天。
setHours(hour,min,sec,millisec): 设置指定的时间的小时字段。该方法可用于设置分钟,秒以及毫秒数。
setMinutes(min,sec,millisec): 用于设置指定时间的分钟字段。该方法同样可用于设置秒数与毫秒数。
setSeconds(sec,millisec): 用于设置日期对象的秒字段。可以用于设置毫秒数。
setMilliseconds(): 用于设置指定时间的毫秒字段。(0-999)
(三)转换为字符串
1、
toString(): 把 Date 对象转换为字符串,并返回结果。
var date1 = new Date();
var date = new Date("2018-12-06 11:05:45");
console.log(date1);//Thu Dec 06 2018 15:11:13 GMT+0800 (中国标准时间)
console.log(date);//Thu Dec 06 2018 11:05:45 GMT+0800 (中国标准时间)
console.log(date1.toString());//Thu Dec 06 2018 15:11:13 GMT+0800 (中国标准时间)
console.log(date.toString());//Thu Dec 06 2018 11:05:45 GMT+0800 (中国标准时间)
console.log(typeof date1);//Object
console.log(typeof date);//Object
console.log(typeof date1.toString());//string
console.log(typeof date.toString());//string
toUTCString(): 返回对应的UTC时间,也就是比北京时间晚8个小时。
toISOString(): 方法返回对应时间的ISO8601写法。
var date1 = new Date();
var date = new Date("2018-12-06 11:05:45");
console.log(date1);//Thu Dec 06 2018 15:11:13 GMT+0800 (中国标准时间)
console.log(date);//Thu Dec 06 2018 11:05:45 GMT+0800 (中国标准时间)
console.log(date1.toUTCString());//Thu, 06 Dec 2018 07:13:50 GMT
console.log(date.toUTCString());//Thu, 06 Dec 2018 03:05:45 GMT
console.log(typeof date1.toUTCString());//string
console.log(typeof date.toUTCString());//string
2、
toDateString(): 返回日期的字符串形式
toTimeString(): 返回时间的字符串形式。
toLocaleDateString(): 返回一个字符串,代表日期的当地写法
toLocaleTimeString(): 返回一个字符串,代表时间的当地写法
var date = new Date("2018-12-06 11:05:45");
console.log(date);//Thu Dec 06 2018 11:05:45 GMT+0800 (中国标准时间)
console.log(date.toDateString());//Thu Dec 06 2018
console.log(typeof date.toDateString());//string
console.log(date.toTimeString());//11:05:45 GMT+0800 (中国标准时间)
console.log(typeof date.toTimeString());//string
console.log(date.toLocaleDateString());//2018/12/6
console.log(typeof date.toLocaleDateString());//string
console.log(date.toLocaleTimeString());//上午11:05:45
console.log(typeof date.toLocaleTimeString());//string
(四)Date.now()
Date.now(): 返回当前距离1970年1月1日00:00:00的毫秒数
console.log(Date.now());//1544090353139
(五)parse()
parse():解析日期字符串,返回距离1970年1月1日 00:00:00的毫秒数
注:
- 日期字符串的格式应该完全或者部分符合YYYY-MM-DDTHH:mm:ss.sssZ格式,Z表示时区,选填
- 若解析失败,返回NaN
Date.parse(datestring)
//参数:必填。表示日期和时间的字符串。
console.log(Date.parse("January 26, 2011 13:51:50"));//1296021110000
console.log(Date.parse("Mon, 25 Dec 1995 13:30:00 GMT"));//819898200000
console.log(Date.parse("Mon, 25 Dec 1995 13:30:00 +0430"));//819882000000
console.log(Date.parse("2011-10-10"));//1318204800000
console.log(Date.parse("2011-10-10 20:00:00"));//1318248000000
console.log(Date.parse("2011-10-10T14:48:00"));//1318229280000
五、示例
倒计时:
<body onload="countTime()">
<div>
<span id="dayDate">00</span>
<span id="hourDate">00</span>
<span id="minuteDate">00</span>
<span id="secondDate">00</span>
</div>
<script type="text/javascript">
function countTime() {
//获取当前时间
var date = new Date();
var now = date.getTime();
console.log(date,"=====",now);
//设置截止时间
var str="2018/12/18 16:50:03";
var endDate = new Date(str);
var end = endDate.getTime();
console.log(endDate,"=====",end);
//时间差
var timeGap = end-now;
//定义变量 d,h,m,s保存倒计时的时间
var d,h,m,s;
if (timeGap>=0) {
d = Math.floor(timeGap/1000/60/60/24);
h = Math.floor(timeGap/1000/60/60%24);
m = Math.floor(timeGap/1000/60%60);
s = Math.floor(timeGap/1000%60);
//递归每秒调用countTime方法,显示动态时间效果
setTimeout(countTime,1000);//计时器
}
else {
d = 0;
h = 0;
m = 0;
s = 0;
}
//将倒计时赋值到div中
document.getElementById("dayDate").innerHTML = d+"天";
document.getElementById("hourDate").innerHTML = h+"时";
document.getElementById("minuteDate").innerHTML = m+"分";
document.getElementById("secondDate").innerHTML = s+"秒";
}
</script>
</body>
六、小练习
- 求去年的今天是周几
- 求三十天前的日期
- 距离 2019-01-01 00:00:00 还剩多少天多少小时多少分多少秒
- 按照如下方式格式化某个历史时间按照如下方式格式化某个历史时间【类似于QQ,微信聊天窗口中的刚刚,几天前等】
- 刚刚 (距当前时间小于一分钟)
- xx 分钟前 (大于等于1分钟,小于1个小时)
- xx 小时前 (大于等于1小时,小于1天)
- xx 天前 (大于等于1天,小于1个月)
- xx 月前 (大于等于1月,小于1年)
- xx 年前 (大于等于1年)
扩展:小小的普及一下日期的一些英文单词 O(∩_∩)O~
星期 | 音标 | 缩写 | |
---|---|---|---|
Monday | 英 /'tjuːzdeɪ; -dɪ/ 美 /ˈtuzdɪ;-de/ | 星期一 | Mon |
Tuesday | 英 /'tjuːzdeɪ; -dɪ/ 美 /ˈtuzdɪ;-de/ | 星期二 | Tue |
Wednesday | 英 /ˈwenzdɪ/ 美 /ˈwɛnzdɪ/ | 星期三 | Wed |
Thursday | 英 /'θɜːzdeɪ; -dɪ/ 美 /ˈθɝzde; ˈθɝzdɪ/ | 星期四 | Thu |
Friday | 英 /ˈfraɪdeɪ/ | 星期五 | Fri |
Saturday | 英 /ˈsætədɪ; -deɪ/ 美 /sætɚdɪ; -de/ | 星期六 | Sat |
Sunday | 英 /ˈsʌndeɪ; ˈsʌndi/ 美 /ˈsʌnde; ˈsʌndi/ | 星期日 | Sun |
月份 | 音标 | 缩写 | |
---|---|---|---|
January | 英 /'dʒænjʊ(ə)rɪ/ 美 /'dʒænjʊ’ɛri/ | 一月 | Jan |
February | 英 /ˈfebrʊərɪ/ 美 /ˈfebrʊerɪ;'fɛbrʊɛrɪ/ | 二月 | Feb |
March | 英 /mɑːtʃ/ 美 /mɑrtʃ/ | 三月 | Mar |
Thursday | 英 /'θɜːzdeɪ; -dɪ/ 美 /ˈθɝzde; ˈθɝzdɪ/ | 四月 | Thu |
Friday | 英 /ˈfraɪdeɪ/ | 五月 | Fri |
June | 英 /dʒu:n/ 美 /dʒun/ | 六月 | Jun |
July | 英 /dʒʊˈlaɪ/ 美 /dʒʊˈlaɪ/ | 七月 | Jul |
August | 英 /ˈɔːɡəst/ 美 /ɔˈgʌst/ | 八月 | Aug |
September | 英 /sep’tembə/ 美 /sɛp’tɛmbɚ/ | 九月 | Sep |
October | 英 /ɒk’təʊbə/ 美 /ɑk’tobɚ/ | 十月 | Oct |
November | 英 /nə(ʊ)'vembə/ 美 /no’vɛmbɚ/ | 十一月 | Nov |
December | 英 /dɪ’sembə/ 美 /dɪ’sɛmbɚ/ | 十二月 | Dec |