年周转换为时间戳
1.方法
// 时间戳转年月日 参数是秒的时间戳 函数返回一个对象 对象里有年 月 日
export function yearDay (long) {
var time = new Date(long * 1000)
var year = time.getFullYear();
var month = (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1);
var date = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
var yearday = { year, month, date }
return yearday
}
// 年周转换为时间戳
/*
计算一年中的每一周都是从几号到几号
第一周为1月1日到 本年的 第一个周日
第二周为 本年的 第一个周一 往后推到周日
以此类推 再往后推52周。。。
如果最后一周在12月31日之前,则本年有垮了54周,反之53周
12月31 日不论是周几,都算为本周的最后一天
参数年份 ,函数返回一个数组,数组里的对象包含 这一周的开始日期和结束日期
*/
export function yearWeekChangeStr (yearWeek, dayFlag) {
if (!yearWeek) return ''
if (yearWeek.length > 7) return ''
const year = yearWeek.substr(0, 4)
let week = yearWeek.substr(4, 2)
week = week < 10 ? +week : week
let day = yearWeek.substr(6)
day = day === '' ? 1 : day
var d = new Date(year, 0, 1);
while (d.getDay() != 1) {
d.setDate(d.getDate() + 1);
}
const arr = []
const longnum = d.setDate(d.getDate())
if (longnum > +new Date(year, 0, 1)) {
const obj = yearDay(+new Date(year, 0, 1) / 1000)
obj.last = yearDay(longnum / 1000 - 86400)
arr.push(obj)
}
const oneitem = yearDay(longnum / 1000)
oneitem.last = yearDay(longnum / 1000 + 86400 * 6)
arr.push(oneitem)
var lastStr
for (var i = 0; i < 51; i++) {
const long = d.setDate(d.getDate() + 7)
const obj = yearDay(long / 1000)
obj.last = yearDay(long / 1000 + 86400 * 6)
lastStr = long + 86400000 * 6
arr.push(obj)
}
if (lastStr < +new Date(year + 1, 0, 1)) {
const obj = yearDay(lastStr / 1000 + 86400)
obj.last = yearDay(+new Date(year + 1, 0, 1) / 1000 - 86400)
arr.push(obj)
} else {
arr[arr.length - 1].last = yearDay(+new Date(year + 1, 0, 1) / 1000 - 86400)
}
const { month, date } = arr[week]
const date_str = dayFlag ? +date + +(day == 0 ? 7 : +day - 1) : date
const resultStr = +new Date(year + '-' + month + '-' + date_str)
return resultStr
}
- 使用
yearWeekChangeStr('202312') // 年周转时间戳
yearWeekChangeStr('2023121', 'week') // 年周周转时间戳
时间戳转换为年周
1.方法
/*
a = d = 当前日期
b = 6 - w = 当前周的还有几天过完(不算今天)
a + b 的和在除以7 就是当天是当前月份的第几周
*/
export function getYearWeek (date, dayFlag) {
if (!date) return ''
/*
date1是当前日期
date2是当年第一天
d是当前日期是今年第多少天
用d + 当前年的第一天的周差距的和在除以7就是本年第几周
*/
const today = new Date(date); // 获取当前时间
const year = today.getFullYear();
const a = today.getYear();
const b = today.getMonth() + 1;
const c = today.getDate();
const day = today.getDay();
const date1 = new Date(a, parseInt(b) - 1, c)
const date2 = new Date(a, 0, 1)
const d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
const week = Math.ceil(
(d + ((date2.getDay() + 1) - 1)) / 7
);
const text = year + '' + (week < 10 ? '0' + week : week)
return dayFlag ? text + '' + day : text
}
- 使用
getYearWeek(this.searchForm.structureWeek) // 时间戳转年周
getYearWeek(this.searchForm.factoryCompleteDate, 'week') // 时间戳转年周周
- 时间戳转换为年周(时间选择框展示年周)
<el-date-picker
v-model="searchForm.structureWeek" type="week"
placeholder="年周"
:format="`${structureWeek}`"></el-date-picker>
computed: {
structureWeek () {
return getYearWeek(this.searchForm.structureWeek)
},
factoryCompleteDate () {
return getYearWeek(this.searchForm.factoryCompleteDate, 'week')
}
}