创建date
1.当前时间
let date = new Date();
2.自定义生成的时间
let date = new Date(yyyy, mm, dd);
let date1 = new Date('2019-03-23');
常用的一些时间的操作
- 从 Date 对象以四位数字返回年份
date.getFullYear(); //格式为四位的完整年 ps: 2019 - 从 Date 对象返回月份 (0 ~ 11)
date.getMonth(); - 从 Date 对象返回一个月中的某一天 (1 ~ 31)
date.getDate();
3.1 如果想获取某个月有多少天(其实就是获取到这个月的最后一天是哪一天)
操作的方法就是:ps:获取2020-06的天数
date.getDate(2020, 7, 0);
最后一位如果小于1的话,默认会获取上个月的最后一天的日期,也就可以获取到6月份的天数了 - 从 Date 对象返回一周中的某一天 (0 ~ 6)
date.getDay(); - 返回 Date 对象的小时 (0 ~ 23)
date.getHours(); - 返回 Date 对象的分钟 (0 ~ 59)
date.getMinutes(); - 返回 Date 对象的秒数 (0 ~ 59)
date.getSeconds(); - 返回 Date 对象的毫秒(0 ~ 999)
date.getMilliSeconds(); - 返回 1970 年 1 月 1 日至今的毫秒数
date.getTime(); - 返回本地时间与格林威治标准时间 (GMT) 的分钟差
date.getTimezoneOffset(); - 方法可解析一个日期时间字符串,并返回 1970/1/1 午夜距离该日期时间的毫秒数
Date.parse('2020-06-11 10:29:10') // 1591842550000 - 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数
Date.UTC(year,month,day,hours,minutes,seconds,ms)
year, month, day 这三项是必填项 - 把 Date 对象的时间部分转换为字符串
date.toTimeString()
13.1 根据本地时间格式,把 Date 对象的时间部分转换为字符串
date.toLocaleTimeString(); - 吧 Date 对象的日期部分转换为字符串
date.toDateString
14.1 根据本地时间格式,把 Date 对象的日期部分转换为字符串
date.toLocaleDateString() - 把 Date 对象转换为字符串
date.toString()
15.1 根据本地格式转换为字符串
date.toLocaleString();
网友评论