@TOC
写在前面
实际开发过程中,我们有许许多多的产品都设计到了时间,也有时候需要自己转化时间格式,这里推荐一个日期时间处理类库momentjs,但很多时候,我们仅仅是转化个别时间,还没必要安装这么一个依赖;这篇文章将在未来持续更新,搜罗各种关于时间转化的方法
一 已知开始时间和结束时间 计算出中间的所有日期
// 中国标准时间format yyyy-mm-dd
const format = (time) => {
let ymd = ''
let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
ymd += time.getFullYear() + '-' // 获取年份。
ymd += mouth + '-' // 获取月份。
ymd += day // 获取日。
return ymd // 返回日期。
}
export const getAllDate = (start, end) => {
let dateArr = []
let startArr = start.split('-')
let endArr = end.split('-')
let db = new Date()
db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
let de = new Date()
de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
let unixDb = db.getTime()
let unixDe = de.getTime()
let stamp
const oneDay = 24 * 60 * 60 * 1000;
for (stamp = unixDb; stamp <= unixde;) { datearr.push(format(new date(parseint(stamp)))) stamp="stamp" + oneday } return datearr ... 使用 console.log(getalldate('2018-12-12', '2019-3-3')) < code>=>
结果如下:
二 最近七天 一个月 三个月 一年
最近七天:
// 中国标准时间format yyyy-mm-dd
const format = (time) => {
let ymd = ''
let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
ymd += time.getFullYear() + '-' // 获取年份。
ymd += mouth + '-' // 获取月份。
ymd += day // 获取日。
return ymd // 返回日期。
}
export const getWeekDate = () => {
let myDate = new Date()
// 获取前一周时间
const oneDay = 24 * 60 * 60 * 1000;
let oneweekdate = new Date(myDate - 7 * oneDay)
let lastWeek = []
lastWeek.push(format(oneweekdate))
lastWeek.push(format(myDate))
return lastWeek
}
...
// 使用
console.log(getWeekDate())
控制台输出结果:
最近一个月:
// 中国标准时间format yyyy-mm-dd
const format = (time) => {
let ymd = ''
let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
ymd += time.getFullYear() + '-' // 获取年份。
ymd += mouth + '-' // 获取月份。
ymd += day // 获取日。
return ymd // 返回日期。
}
export const getMonthDate = () => {
let nowDate = new Date()
let nowDateChange = new Date()
let lastMonth = []
// 获取前一月时间
nowDateChange.setMonth(nowDateChange.getMonth() - 1)
lastMonth.push(format(nowDateChange))
lastMonth.push(format(nowDate))
return lastMonth
}
...
// 使用
console.log(getMonthDate())
输出如下:
最近三个月
// 中国标准时间format yyyy-mm-dd
const format = (time) => {
let ymd = ''
let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
ymd += time.getFullYear() + '-' // 获取年份。
ymd += mouth + '-' // 获取月份。
ymd += day // 获取日。
return ymd // 返回日期。
}
export const getThreeMonthDate = () => {
let nowDate = new Date()
let nowDateChange = new Date()
let threeMonth = []
// 获取前三月时间
nowDateChange.setMonth(nowDateChange.getMonth() - 3)
threeMonth.push(format(nowDateChange))
threeMonth.push(format(nowDate))
return threeMonth
}
...
// 使用
console.log(getThreeMonthDate())
输出如下:
一年
const format = (time) => {
let ymd = ''
let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
ymd += time.getFullYear() + '-' // 获取年份。
ymd += mouth + '-' // 获取月份。
ymd += day // 获取日。
return ymd // 返回日期。
}
export const getYearDate = () => {
let nowDate = new Date()
let nowDateChange = new Date()
let lastYear = []
// 获取前一年时间
nowDateChange.setFullYear(nowDateChange.getFullYear()- 1)
lastYear.push(format(nowDateChange))
lastYear.push(format(nowDate))
return lastYear
}
...
// 使用
console.log(getYearDate())
输出如下:
关于JavaScript Date 对象的方法详情请转@W3 school
更多文章请看我的博客@王一诺 感谢阅读!