前言:
今日把之前关于date的笔记做了一个总结,里面有date的各种方法以及常用的格式转换,已备日后参考。
一、 new Date() 获取本地当前时间/可设置指定时间
例1-当前时间:
let myDate=new Date();
console.log(myData);// Wed Oct 18 2017 16:56:32 GMT+0800 (中国标准时间)
例2-指定时间:
let myDate=new Date(2008,08,08);
console.log(myDate); // Mon Sep 08 2008 00:00:00 GMT+0800 (中国标准时间)
以下2-7为各种时间转换成字符串的格式,请根据需求使用
二、toLocaleString() 中国时间格式
(根据本地时间把Date对象转换为字符串,并返回结果)
let myDate=new Date();
console.log(myDate.toLocaleString()) // "2017/10/18 下午4:58:42"
三、toString()把Date对象转为英文的字符串
let myDate=new Date();
console.log(myDate.toString()) // "Wed Oct 18 2017 17:08:09 GMT+0800 (中国标准时间)"
四、toTimeString() 转换为24小时制时间字符串
let myDate=new Date();
console.log(myDate.toTimeString()) // "18:22:12 GMT+0800 (中国标准时间)"
五、toDateString()把Date对象的时间部分转为字符串
let myDate=new Date();
console.log(myDate.toDateString()) // "Thu Oct 19 2017"
六、toUTCString() 把Date对象转换成世界时间的字符串
let date=new Date();
console.log(date.toUTCString()) // "Thu, 19 Oct 2017 06:20:31 GMT"
七、toLocaleTimeString()转为本地am/pm 格式
let date = new Date();
console.log(date.toLocaleTimeString()) // "下午2:20:31"
以下为时、分、秒的各种获取和设置的方法
八、getTime表示当前Date距1970年1月1日午夜的毫秒数/时间戳
getTime() 同:Date.now()
例:
let myDate=new Date();
console.log(myDate.getTime()) // 1508317956004
console.log(Date.now()) // 1508319448166
九、getFullYear() 返回一个表示年份的数字,获取年份
let myDate=new Date();
console.log(myDate.getFullYear()); // 2017
十、setFullYear()更改年份,设置年份
let myDate=new Date();
myDate.setFullYear(1992);
console.log(myDate) // Mon Jul 13 1992 11:55:28 GMT+0800 (中国标准时间)
十一、getMonth()返回月份,获取月份
返回的月份是(0-11),0表示1月,11表示12月,所以需要在返回的值后加1,才是当前月份
let myDate=new Date();
console.log(myDate.getMonth()+1); // 10
十二、setMonth()更改月份,设置月份
设置为8 ,返回给没有Date的是9月,会自动加1
let myDate=new Date();
myDate.setMonth(8);
console.log(myDate); // Wed Sep 13 2017 14:02:44 GMT+0800 (中国标准时间)
十三、getDate()返回当前日(1-31)天数
let myDate=new Date();
console.log(myDate.getDate()) // 18
十四、setDate()方法设置某一天
let myDate=new Date();
myDate.setDate(25);
console.log(myDate) // Tue Jul 25 2017 14:20:18 GMT+0800 (中国标准时间)
十五、getDay()方法为获取星期的某一天的数字
返回0-6,0为周日,1-6为正常星期
let myDate=new Date();
let week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
console.log(week[myDate.getDay()]) // 星期四
十六、getHours()方法返回当前小时
let myDate=new Date();
console.log(myDate.getHours()) // 17
十七、setHours()方法用于设置/更改小时()
返回值为0-23
let myDate=new Date();
myDate.setHours(9)
console.log(myDate) // Thu Jul 13 2017 09:37:31 GMT+0800 (中国标准时间)
十八、getMinutes()方法返回时间的分钟字段
let myDate=new Date();
console.log(myDate.getMinutes()) // 14
十九、setMinutes()方法用于设置分钟字段,范围为0-59
setMinutes(分,秒,毫秒)
let myDate=new Date();
myDate.setMinutes(55,12,5)
console.log(myDate) // Thu Jul 13 2017 15:55:12 GMT+0800 (中国标准时间)
也可以只传一个值:
let myDate=new Date();
myDate.setMinutes(55);
console.log(myDate) // Wed Oct 18 2017 18:55:13 GMT+0800 (中国标准时间)
二十、getSeconds()方法获取时间的秒 范围为0-59
let myDate=new Date();
console.log(myDate.getSeconds()) // 58
二十一、setSenconds()方法用于更改秒
let myDate=new Date();
myDate.setSeconds(12)
console.log(myDate) // Thu Jul 13 2017 15:15:12 GMT+0800 (中国标准时间)
二十二、getMilliseconds() 获取毫秒
let myDate=new Date();
console.log(myDate.getMilliseconds()) // 147
二十三、setMilliseconds()更改毫秒
let myDate=new Date();
myDate.setMilliseconds(789)
console.log(myDate.getMilliseconds()) // 789
-----------------------下面写两个最常用的方法--------------
二十四、获取当前时间 /时间戳
获取当前时间/时间戳 年月日,时分秒:
let myDate=new Date(); //获取当前时间的年、月、日 、时、分、秒、
或者你需要把一个时间戳转为年、月、日、时、分、秒、的格式。
let myDate=new Date(1499931534351 );
function time(myDate) {
let year=myDate.getFullYear(); //年
let mouth=myDate.getMonth()+1; //月
let date=myDate.getDate(); //日
let hours=myDate.getHours(); //时
let minutes=myDate.getMinutes(); //分
let seconds=myDate.getSeconds(); //秒
console.log("当前日期为:"+year+"年"+","+mouth+"月"+","+date+"日"+" "+hours+"时"+":"+minutes+"分"+":"+seconds+"秒")
}
time(myDate)
// 当前日期为:2017年,7月,13日 15时:34分:26秒 (当前时间)
// 当前日期为:2017年,7月,13日 15时:38分:54秒 (时间戳)
二十五、制作一个每秒变化的时间
<div class="clock"></div>
function time() {
let myDate=new Date();
let hours=myDate.getHours();
let minutes=myDate.getMinutes();
let seconds=myDate.getSeconds();
let obj=document.querySelector(".clock");
obj.innerHTML=hours+":"+minutes+":"+seconds;
let t=setTimeout("time()",1000)
}
time() // 15:56:42
二十六、与业务相关,快捷获取某些时间的毫秒时间戳
1. 获取指定日期
当我们需要一个指定日期的时间戳,例如:2008年01月01日
new Date('2008,01,01').getTime() // 获取的结果是:1199116800000 --- 2008/1/1 0:0:0 的毫秒时间戳
2. 获取当日具体的某时某分某秒
获取当日的0时0分0秒的时间戳
new Date().setHours(0, 0, 0) // 获取的结果是:1590940800743 --- 2020/6/1 0:0:0
获取当日的23点59分59秒的时间戳
new Date().setHours(23, 59, 59) // 获取的结果是:1591027199895 --- 2020/6/1 23:59:59
3. 获取此刻的时间戳,两种都可以
new Date().getTime() // 获取结果是:1591000349527 --- 2020/6/1 16:32:29
new Date().valueOf() // 获取结果是:1591001432484 --- 2020/6/1 16:50:32
二十七、dayjs:一个较轻量级的处理日期的js库
上面的操作中,我们都是在处理原生的
Date对象,下面介绍一个我比较常用的日期处理的js库:
dayjs: 一个轻量的处理时间和日期的 JavaScript 库
Moment.js 的 2kB 轻量化方案,拥有同样强大的 API
0. 获取时间戳
- 秒
const time = dayjs('2020-06-25').unix();
console.log(time); // 1593014400
- 毫秒
const time = dayjs('2020-06-25').valueOf();
console.log(time); // 1593014400000
1. 时间戳转为格式化的格式
| 输入 | 例子 | 解析 |
|---|---|---|
| YY | 20 | 两位数的年份 |
| YYYY | 2020 | 四位数的年份 |
| M | 1-12 | 月份,从1开始 |
| MM | 01-12 | 月份,两位数 |
| MMM | January-December | 完整的月份名称 |
| D | 1-31 | 月份里的一天 |
| DD | 01-31 | 月份里的一天,两位数 |
| H | 0-23 | 小时 |
| HH | 00-23 | 小时,两位数 |
| h | 1-12 | 小时,12时制 |
| hh | 01-12 | 小时,12小时制,两位数 |
| m | 0-59 | 分钟 |
| mm | 00-59 | 分钟,两位数 |
| s | 0-59 | 秒 |
| ss | 00-59 | 秒,两位数 |
| S | 0-9 | 毫秒,一位数 |
| SS | 00-99 | 毫秒,两位数 |
| SSS | 000-999 | 毫秒,三位数 |
| A | AM PM | 上午,下午,大写 |
| a | am pm | 上午,下午,小写 |
下面我们可以根据场景,选择组合:
- 较常用的日期格式
dayjs(1591001432484).format('YYYY-MM-DD') // 2020-06-01
dayjs(1591001432484).format('YYYY-MM-DD hh:mm:ss') // 2020-06-01 16:50:32
dayjs(1591001432484).format('MM-DD-YYYY') // 06-01-2020
dayjs().format('DD/MM/YYYY') // 02/06/2020
- 当我们想在时间的格式化中添加一些字符串,用中括号包起来即可:[字符串],也可以穿插在里面
const time = dayjs().format('[时间:] YYYY-MM-DD HH:mm:ss');
console.log(time) // 时间: 2020-06-02 11:54:25
const time = dayjs().format('m分钟s秒');
console.log(time); // 57分钟58秒
- 带am pm
const time = dayjs().format('YYYY-MM-DD h:mm A');
console.log(time); // 2020-06-02 2:27 PM
2. 设置指定时间
const time = dayjs()
.year(2000)
.hour(12)
.minute(59)
.second(59)
.valueOf();
console.log(time); // 959835599490 --- 2000/6/1 12:59:59
const time = dayjs()
.set('year', 2000)
.set('month', 1)
.set('day', 5)
.set('hour', 2)
.set('minute', 32)
.set('second', 22)
.valueOf();
console.log(time); // 949602742781 --- 2000/2/4 2:32:22
3. 增加一定时间
const time = dayjs()
.add(1, 'year')
.add(2, 'month')
.add(7, 'day')
.add(3, 'hour')
.add(1, 'minute')
.add(5, 'second')
.valueOf();
console.log(time); // 1628489163253 --- 2021/8/9 14:6:3
4. 减少一定时间
const time = dayjs()
.subtract(1, 'year')
.subtract(2, 'month')
.subtract(7, 'day')
.subtract(3, 'hour')
.subtract(1, 'minute')
.subtract(5, 'second')
.valueOf();
console.log(time); // 1553559096762 --- 2019/3/26 8:11:36
5. 设置到一个时间的开始
- 设置到今年1月1日上午 00:00
const time = dayjs()
.startOf('year')
.valueOf();
console.log(time); // 1577808000000 --- 2020/1/1 0:0:0
- 设置到本月1日上午 00:00
const time = dayjs()
.startOf('month')
.valueOf();
console.log(time); // 1590940800000 --- 2020/6/1 0:0:0
- 设置到当天 00:00
const time = dayjs()
.startOf('date')
.valueOf();
console.log(time); // 1591027200000 --- 2020/6/2 0:0:0
设置到当前时间,0 分、0 秒、0 毫秒
const time = dayjs()
.startOf('hour')
.valueOf();
console.log(time); // 1591066800000 --- 2020/6/2 11:0:0
- 设置到当前时间,0 秒、0 毫秒
const time = dayjs()
.startOf('minute')
.valueOf();
console.log(time); // 1591068420000 --- 2020/6/2 11:27:0
- 设置到当前时间,0 毫秒
const time = dayjs()
.startOf('second')
.valueOf();
console.log(time); // 1591068563000 --- 2020/6/2 11:29:23
6. 设置到一个结束时间
同上面的方法相反,这个方法拿到的是结束时间 year / month / day / hour / minute / second
const time = dayjs()
.endOf('month')
.valueOf();
console.log(time); // 1593532799999 --- 2020/6/30 23:59:59
7. Difference 返回指定单位下两个日期时间之间的差异。
- 默认返回的是差异单位为毫秒
const date1 = dayjs('2019-01-25');
const date2 = dayjs('2018-06-05');
console.log(date1.diff(date2)); // 20214000000 默认单位是毫秒
- 可添加想判断的差异单位 year / month / day / hour / minute / second
const date1 = dayjs('2029-01-25');
const date2 = dayjs('2020-06-05');
console.log(date1.diff(date2, 'year')); // 8
8. daysInMonth 获取月份中有多少天
const time = dayjs('2019-01-25').daysInMonth();
console.log(time); // 31
** 完 **
如果你对我对文章感兴趣或者有些建议想说给我听👂,也可以添加一下微信哦!
如果感觉我的文章还不错的话,可以一下添加关注哦!
最后:
祝各位工作顺利!
-小菜鸟Christine