被日历折磨了3天,发现关于Date的基本转换在此时显得格外重要!现在来记录一下关于这个部分的相关知识,用烂笔头来弥补自己的坏记性。
一 关于日期的基础知识
首先创建一个date对象
new Date() // Fri Nov 10 2017 14:20:37 GMT+0800 (CST)
然后来看看它支持哪些方法吧
// 下面为常用方法
var date = new Date()
date.getDate() // 返回月份的某一天
date.getDay() // 返回一周中的某一天
date.getMonth() // 返回一年中的某一个月(注意这里比你真实希望的结果要慢一个月)
date.getFullYear() // 获取到年份
date.getHours() // 小时 (0 ~ 23)
date.getMinutes() // 分钟(0~59)
date.getSeconds() // 秒(0~59)
date.getMilliseconds() // 毫秒(0~999)
date.getTime() // 返回1970年1月1日到现在的毫秒数(就是你要获取的时间戳)
date.valueOf() // 返回 Date 对象的原始值(和上面的值一致)
Date.parse(date) // 1970年1月1日午夜到指定日期(字符串)的毫秒数。例如1510296981000
// 下面的方法用的相对比较少
date.setDate(15) // 设置当月的某一天
// 相应的Month,FullYear,Hours,Minutes,Seconds,Milliseconds也有相同的方法
date.setTime() //以毫秒设置 Date 对象
// 例如:day.setTime(1510748531000) // 1510748531000
date.getUTCDate() // 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)
// 相应的Month,FullYear,Hours,Minutes,Seconds,Milliseconds也有相同
date.setUTCDate(15) // 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)
// 相应的Month,FullYear,Hours,Minutes,Seconds,Milliseconds也有相同
Date.UTC(2017,10,11) // 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数
上面罗列的方法都是Date对象原生支持的方法,如果有需要也可以在控制台直接查看。
二 关于date的常用方法总结
- 获取时间戳,只精确到天,时分秒全部置为0,不传递参数返回当天0点的(一般用于比较是否早/晚于今天)
function initTimeDate (date) {
date = date || new Date()
date.setHours(0)
date.setMinutes(0)
date.setSeconds(0)
return date.getTime()
}
- format
仅支持下面几种转化形式,如果有频繁的复杂的转化推荐使用date-fns
代码 | 含义 | 举例 |
---|---|---|
yyyy | 年份 | 2017 |
yy | 年份 | 17 |
M | 月份 | 1-12 |
MM | 月份 | 01-12 |
D | 天 | 1-31 |
DD | 天 | 01-31 |
H | 小时 | 0-23 |
HH | 小时 | 00-23 |
h | 小时 | 1-12 |
m | 分钟 | 0-59 |
mm | 分钟 | 00-59 |
s | 秒 | 0-59 |
ss | 秒 | 00-59 |
SSS | 毫秒 | 000-999 |
// lib.js
/*这个函数用来填补0*/
function addLeadingZeros (number, targetLength) {
var output = Math.abs(number).toString()
while (output.length < targetLength) {
output = '0' + output
}
return output
}
/*支持的函数形式*/
const formatters = {
// Year: 1900, 1901, ..., 2099
'yyyy': function (date) {
return addLeadingZeros(date.getFullYear(), 4)
},
// Year: 00, 01, ..., 99
'yy': function (date) {
return addLeadingZeros(date.getFullYear(), 4).substr(2)
},
// Month: 1, 2, ..., 12
'M': function (date) {
return date.getMonth() + 1
},
// Month: 01, 02, ..., 12
'MM': function (date) {
return addLeadingZeros(date.getMonth() + 1, 2)
},
// Day of month: 1, 2, ..., 31
'D': function (date) {
return date.getDate()
},
// Day of month: 01, 02, ..., 31
'DD': function (date) {
return addLeadingZeros(date.getDate(), 2)
},
// Hour: 0, 1, ... 23
'H': function (date) {
return date.getHours()
},
// Hour: 00, 01, ..., 23
'HH': function (date) {
return addLeadingZeros(date.getHours(), 2)
},
// Hour: 1, 2, ..., 12
'h': function (date) {
var hours = date.getHours()
if (hours === 0) {
return 12
} else if (hours > 12) {
return hours % 12
} else {
return hours
}
},
// Minute: 0, 1, ..., 59
'm': function (date) {
return date.getMinutes()
},
// Minute: 00, 01, ..., 59
'mm': function (date) {
return addLeadingZeros(date.getMinutes(), 2)
},
// Second: 0, 1, ..., 59
's': function (date) {
return date.getSeconds()
},
// Second: 00, 01, ..., 59
'ss': function (date) {
return addLeadingZeros(date.getSeconds(), 2)
},
// Millisecond: 000, 001, ..., 999
'SSS': function (date) {
return addLeadingZeros(date.getMilliseconds(), 3)
}
}
format (fmt, date) {
date = new Date(date)
const reg = /d{1,4}|M{1,4}|yy(?:yy)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g
const result = fmt.replace(reg, function ($0) {
return formatters[$0](date)
})
return result
}
// 测试
console.log(format('yyyy-MM-DD', new Date()))
console.log(format('yyyy年MM月DD日 HH:mm:ss', 1510990245000))
console.log(format('h:mm:ss', 1510990245000))
console.log(format('h:mm:ss SSS', 1510718319798))