Date() 返回当日的日期和时间。 | |
getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。 | |
getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。 | |
getMonth() 从 Date 对象返回月份 (0 ~ 11)。 | |
getFullYear() 从 Date 对象以四位数字返回年份。 | |
getYear() 请使用 getFullYear() 方法代替。 | |
getHours() 返回 Date 对象的小时 (0 ~ 23)。 | |
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。 | |
getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。 | |
getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。 | |
getTime() 返回 1970 年 1 月 1 日至今的毫秒数。 | |
getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。 | |
getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。 | |
getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。 | |
getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。 | |
getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。 | |
getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。 | |
getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。 | |
getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。 | |
getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。 | |
parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。 | |
setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。 | |
setMonth() 设置 Date 对象中月份 (0 ~ 11)。 | |
setFullYear() 设置 Date 对象中的年份(四位数字)。 | |
setYear() 请使用 setFullYear() 方法代替。 | |
setHours() 设置 Date 对象中的小时 (0 ~ 23)。 | |
setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。 | |
setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。 | |
setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。 | |
setTime() 以毫秒设置 Date 对象。 | |
setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。 | |
setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。 | |
setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。 | |
setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。 | |
setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。 | |
setUTCSeconds() 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。 | |
setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。 | |
toSource() 返回该对象的源代码。 | |
toString() 把 Date 对象转换为字符串。 | |
toTimeString() 把 Date 对象的时间部分转换为字符串。 | |
toDateString() 把 Date 对象的日期部分转换为字符串。 | |
toGMTString() 请使用 toUTCString() 方法代替。 | |
toUTCString() 根据世界时,把 Date 对象转换为字符串。 | |
toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。 | |
toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。 | |
toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 | |
UTC() 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。 | |
valueOf() 返回 Date 对象的原始值。 |
-----整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时UTC(UTC, Universal Time Coordinated),UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同北京时区是东八区:+0800,领先UTC八个小时
控制台有颜色就是数字。有的在控制台看是字符串,但是可能是对象。字符串没有折叠面板
----new Date(ms)
//把毫秒数转换为Date对象,表示从'1970/01/01 00:00:00'为起点,开始叠加的毫秒数,注意:起点的时分秒还要加上当前所在的时区,北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00'
var dt = new Date(1000 * 60 * 1); // 前进1分钟的毫秒数 console.log(dt); // 1970/01/01 08:01:00 dt = new Date(-1000 * 60 * 1); // 倒退1分钟的毫秒数 console.log(dt); //1970/01/01 07:59:00 var dt3=new Date(1000) //可以传ms console.log(dt3) //Thu Jan 01 1970 08:00:01 GMT+0800 (中国标准时间)
var dt=new Date() //在当前运行代码时,创建一个时间点对象 console.log(dt.getDate()) //几号 console.log(dt.getDay()) //周几 周天=7 //下周是多久,就直接+7天,下个月就需要看天数了 console.log(dt.getFullYear()) //4位数的年份 console.log(dt.getYear()) //3位的年 122后两位是年份 console.log(dt.getMonth()) //重点!范围是0-11 所以一般显示时间,是+1 console.log(dt.getMonth()+1) console.log(dt.getHours()) //重点!名字要记得:大写+s。 不能取到24 //笔试题:getHours getHour gethours gethour console.log(dt.getMinutes()) //分钟 console.log(dt.getSeconds()) //秒 console.log(dt.getMilliseconds()) // 1000ms=1s console.log(dt) //Mon May 23 2022 20:12:25 GMT+0800 (中国标准时间) 它是一个对象,不是字符串 console.log("Mon May 23 2022 20:12:25 GMT+0800 (中国标准时间) ") //这是字符串 //可以算那个时间是周几... var dt2=new Date("1999-02-02") console.log(dt2) //Tue Feb 02 1999 08:00:00 GMT+0800 (中国标准时间) console.log(dt.getTime()) //1653308676057 这个代表1970-01.....时间开始多少ms
----new Date(dateStr) //把字符串转换为Date对象
换为Date对象的字符串(可省略时间)的格式主要有两种:
yyyy/MM/dd HH:mm:ss(都用的这种)若省略时间,返回的Date对象的时间为 00:00:00。
yyyy-MM-dd HH:mm:ss若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区),若不省略时间,此字符串在IE中会转换失败
var dt = new Date('2017/12/25'); // yyyy/MM/dd console.log(dt); // 2017/12/25 00:00:00 dt = new Date('2017/12/25 12:00:00'); // yyyy/MM/dd HH:mm:ss console.log(dt); //2017/12/25 12:00:00 dt = new Date('2017-12-25'); // yyyy-MM-dd console.log(dt); //2017-12-25 08:00:00 (加上了东8区的时区) dt = new Date('2017-12-25 12:00:00'); // yyyy-MM-dd HH:mm:ss (注意:此转换方式在IE中会报错!) console.log(dt); // 2017-12-25 12:00:00
----new Date(year, month, opt_day, opt_hours, opt_minutes, opt_seconds, opt_milliseconds)
把年月日、时分秒转换为Date对象//option
参数:
year:年份4位数字,如:1999、2014
month:月份2位数字,从0开始计算,0表示1月份、11表示12月份。
opt_day可选号2位数字,从1开始计算,1表示1号。
opt_hours可选时2位数字,取值0~23。
opt_minutes可选分2位数字,取值0~59。
opt_seconds可选秒2位数字,取值0~59。
opt_milliseconds可选毫秒,取值0~999。
var dt = new Date(2017, 1); // 2017年2月(这里输入的月份数字为1) console.log(dt); // 2017/2/01 00:00:00 dt = new Date(2017, 1, 25); // 2017年2月25日 console.log(dt); //2017/2/25 00:00:00 dt = new Date(2017, 1, 25, 15, 30, 40); // 2017年2月25日 15点30分40秒 console.log(dt); // 2017/2/25 15:30:40 dt = new Date(2017, 12, 25); // 2017年13月25日(这里输入的月份数字为12,表示第13个月,跳转到第二年的1月) console.log(dt); // 2018/01/25
====实例方法
Date对象的方法分为2种形式:本地时间和UTC时间。同一个方法,一般都会有2种时间格式操作(方法名带UTC的,就是操作UTC时间),我们主要学习本地时间的操作。
----get方法
getFullYear() 返回Date对象的年份值;4位年份。
getMonth() 返回Date对象的月份值。从0开始,所以真实月份=返回值+1 。
getDate() 返回Date对象的月份中的日期值;值的范围1~31 。
getHours() 返回Date对象的小时值。
getMinutes() 返回Date对象的分钟值。
getSeconds() 返回Date对象的秒数值。
getMilliseconds()返回Date对象的毫秒值。
getDay() 返回Date对象的一周中的星期值;0为星期天,1为星期一
getTime() 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
dt.getFullYear(); // 2017 dt.getMonth(); //1实际为2月份(月份从0开始计算) dt.getDate(); // 25 dt.getHours(); // 15 dt.getMinutes(); // 36 dt.getSeconds(); // 21 dt.getMilliseconds(); // 125 dt.getDay(); // dt.getTime(); // 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
----set方法
setFullYear(year, opt_month, opt_date)设置Date对象的年份值;4位年份。
setMonth(month, opt_date) 设置Date对象的月份值。0表示1月,11表示12月。
setDate(date)设置Date对象的月份中的日期值;值的范围1~31 。
setHours(hour, opt_min, opt_sec, opt_msec)设置Date对象的小时值。
setMinutes(min, opt_sec, opt_msec)设置Date对象的分钟值。
setSeconds(sec, opt_msec) 设置Date对象的秒数值。
setMilliseconds(msec) 设置Date对象的毫秒值。
var dt = new Date(); dt.setFullYear(2016); // => 2016 dt.setMonth(11); // 11实际为12月份(月份从0开始计算) dt.setDate(25); // 25 dt.setHours(15); //15 dt.setMinutes(30); // 30 dt.setSeconds(40); // 40 dt.setMilliseconds(333); //333 console.log(dt); // 2016年12月25日 15点30分40秒 333毫秒
-----to方法
toString() 将Date转换为一个 '年月日 时分秒' 字符串
toLocaleString() 将Date转换为一个'年月日 时分秒'的本地格式字符串
toDateString() 将Date转换为一个'年月日'字符串
toLocaleDateString() 将Date转换为一个'年月日'的本地格式字符串
toTimeString() 将Date转换为一个'时分秒'字符串
toLocaleTimeString() 将Date转换为一个'时分秒'的本地格式字符串
valueOf() 与getTime()一样,返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
var dt = new Date(); console.log(dt.toString()); // Tue Dec 25 2017 20:56:11 GMT+0800 (中国标准时间) console.log(dt.toLocaleString()); //2017年12月25日 下午8:56:11 console.log(dt.toDateString()); // Tue Dec 23 2014 console.log(dt.toLocaleDateString()); // 2017年12月25日 console.log(dt.toTimeString()); //20:56:11 GMT+0800 (中国标准时间) console.log(dt.toLocaleTimeString()); //下午8:56:11 console.log(dt.valueOf()); //返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
----类方法(静态方法)
Date.now() 返回当前日期和时间的Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
console.log(Date.now()); // 1419431519276
Date.parse(dateStr)把字符串转换为Date对象 ,然后返回此Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
参数2种:
yyyy/MM/dd HH:mm:ss若省略时间(大家都使用这种方法),返回的Date对象的时间为 00:00:00。
yyyy-MM-dd HH:mm:ss若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。若不省略时间,此字符串在IE中返回NaN(非数字)
console.log(Date.parse('2014/12/25 12:00:00')); // 1419480000000
console.log(Date.parse('2014-12-25 12:00:00')); // 1419480000000 (注意:此转换方式在IE中返回NaN)
----两个Date的时间差,返回两者相差的毫秒数
var dt1 = new Date('2012/12/01');
var dt2 = new Date('2018/12/25');
console.log(dt1 > dt2); // false
console.log(dt1-dt2)//返回两者相差的毫秒数
----倒计时:X天X时X分
计算当前时间离目的时间相差多少天时分。
function getDownTime(dt) { // 1.获取倒计时 var ms = dt - Date.now(); // 目的时间减去现在的时间,返回两者相差的毫秒数 var s = parseInt(ms/ 1000%60); // 秒 var day = parseInt(ms/ 3600/1000 / 24); // 天数 var hour = parseInt((ms/1000- day * 24 * 3600) / 3600); // 小时 var m = parseInt((ms/1000- day * 24 * 3600 - hour * 3600) / 60); // 分钟 return '倒计时: ' day + '天' + hour + '时' + m + '分'+s+”秒” } console.log(getDownTime(new Date('2018/1/01')))
创建时间的几种方式
var nowdt=new Date()
var sindt=new Date("Mon May 23 2022 20:12:25 GMT+0800 (中国标准时间)")
var absdt=nowdt-dinsdt
console.log(absdt)
if(absdt<1000*60) {
"刚刚"
}
else if (1000*60<=absdt&&absdt<1000*60*60) {
var re=new Date(absdt).getMinutes()
}
//注意:可以超出,会自动进位
var dt6=new Date(2003,2,34,08,30,40)
console.log(dt6) //Thu Apr 03 2003 08:30:40 GMT+0800 (中国标准时间)
//自动进位了,传入2,系统就会进位到3,3就是四月