日期对象

一、创建 Date 对象

  1.new Date()   当前日期和时间

  2. new Date(milliseconds)      参数(milliseconds):从1970年1月1日00:00:00 UTC开始计算的毫秒数。如果将Unix时间戳作为参数,必须将Unix时间戳乘以1000。

                                                   【unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒】

 3.  new Date(dateString)            参数(dateString):一个日期字符串,返回所对应的时间。所有可以被Date.parse()方法解析的日期字符串,都可以当作Date对象的参数

 4.  new Date(year, month, day, hours, minutes, seconds, milliseconds)    参数(year, month, day, hours, minutes, seconds, milliseconds):对应的年、月、日、小时、分钟、秒和毫秒。

 

二、关于日期的常用方法

    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)

   

   set 

            

setTime(): 以毫秒设置 Date 对象。
setFullYear(): 设置年份。
setMonth(month,day): 设置月份。可用于设置月份中的某一天。
setDate(): 设置一个月的某一天。
setHours(hour,min,sec,millisec): 设置指定的时间的小时字段。该方法可用于设置分钟,秒以及毫秒数。
setMinutes(min,sec,millisec): 用于设置指定时间的分钟字段。该方法同样可用于设置秒数与毫秒数。
setSeconds(sec,millisec): 用于设置日期对象的秒字段。可以用于设置毫秒数。
setMilliseconds(): 用于设置指定时间的毫秒字段。(0-999)

  转换为字符串

           

toString(): 把 Date 对象转换为字符串,并返回结果。

toUTCString(): 返回对应的UTC时间,也就是比北京时间晚8个小时。
toISOString(): 方法返回对应时间的ISO8601写法。

toDateString(): 返回日期的字符串形式

toTimeString(): 返回时间的字符串形式。
toLocaleDateString(): 返回一个字符串,代表日期的当地写法
toLocaleTimeString(): 返回一个字符串,代表时间的当地写法

 

 

Date.now():返回当前距离1970年1月1日00:00:00的毫秒数

 

parse():解析日期字符串,返回距离1970年1月1日 00:00:00的毫秒数

                  tip:日期字符串的格式应该完全或者部分符合YYYY-MM-DDTHH:mm:ss.sssZ格式,Z表示时区,选填

                      若解析失败,返回NaN

        

 倒计时

function dowmTime() {
var endtime = new Date('2019-7-15 11:01');
var nowTime = new Date();
var differSecond = parseInt((endtime - nowTime) / 1000)
if (differSecond <= 0) {
clearInterval(flag)
document.getElementsByClassName('daybox')[0].children[0].innerText = 0;
document.getElementsByClassName('hourbox')[0].children[0].innerText = 0;
document.getElementsByClassName('minutebox')[0].children[0].innerText = 0;
document.getElementsByClassName('secondbox')[0].children[0].innerText = 0;
} else {
var day = Math.floor(differSecond / (24 * 60 * 60))
var hour = Math.floor(differSecond / (60 * 60)) - (day * 24);
var minute = Math.floor(differSecond / 60) - (day * 24 * 60) - (hour * 60);
var second = Math.floor(differSecond) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
if (hour <= 9) {
hour = '0' + hour;
}
if (minute <= 9) {
minute = '0' + minute;
}
if (second <= 9) {
second = '0' + second;
}
document.getElementsByClassName('daybox')[0].children[0].innerText = day;
document.getElementsByClassName('hourbox')[0].children[0].innerText = hour;
document.getElementsByClassName('minutebox')[0].children[0].innerText = minute;
document.getElementsByClassName('secondbox')[0].children[0].innerText = second;

}

}
let flag = setInterval(function () {
dowmTime()
}, 1000)

 

原文:https://blog.csdn.net/ai_u20/article/details/84845482

posted @ 2019-07-04 11:50  carrieLee  阅读(500)  评论(0编辑  收藏  举报