beelink/src/utils/date.ts

91 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-10-12 16:15:31 +08:00
import dayjs from 'dayjs'
2020-10-14 09:10:40 +08:00
interface Date{
day?: number;
list?: Array<unknown>;
}
interface GetDate{
date: Array<Array<Date>>;
year: number;
yue: number;
day: number;
zhou: number;
}
export function getdate(yue?: number): GetDate{
2020-10-12 16:15:31 +08:00
let now = dayjs()
2020-10-12 16:44:22 +08:00
if(yue != undefined){
now = now.month(now.month() + yue);
}
yue = now.month() + 1;
2020-10-12 16:15:31 +08:00
const day = now.date() // 当前天
2020-10-13 09:15:08 +08:00
const zhou = now.day(); // 当前周
2020-10-12 16:15:31 +08:00
now = now.date(1)
const week = now.day(); // 第一天是星期几
now = now.month(now.month() + 1);
now = now.date(0);
const month = now.date(); // 当前月有几天
2020-10-12 16:44:22 +08:00
const year = now.year()
2020-10-12 16:15:31 +08:00
console.log(day,week,month)
let i = 0;
let w = 0;
2020-10-14 09:10:40 +08:00
2020-10-12 16:15:31 +08:00
const date: Array<Array<Date>> = [[]];
while(i < month){
for(w = 0; w < week; w++){
date[0][w] = {};
}
i++;
const zhou = Math.floor((i + w - 1) / 7)
const d = {
day: i
}
if(date[zhou] == undefined){
date[zhou] = []
}
date[zhou].push(d)
}
2020-10-12 16:44:22 +08:00
while(date[date.length - 1].length < 7){
date[date.length - 1].push({})
}
2020-10-12 16:15:31 +08:00
console.log(date)
2020-10-12 16:44:22 +08:00
2020-10-13 09:15:08 +08:00
return {date, year, yue, day, zhou};
2020-10-12 16:15:31 +08:00
}
2020-10-13 16:44:32 +08:00
export function getweek(zhou?: number){
let now = dayjs()
if(zhou != undefined){
now = now.day(now.day() + (zhou * 6));
}
const yue = now.month() + 1;
const day = now.date() // 当前天
zhou = now.day(); // 当前周几
const year = now.year()
interface Date{
day: string;
list?: Array<any>;
}
const date: Array<Date> = [];
for(let i = 0; i < 7; i++){
console.log(i);
now = now.day(i + 1)
date[i] = {day: ""};
date[i].day = now.year() + "-" + (now.month() < 10 ? '0' + now.month() : now.month()) + "-" + (now.date() < 10 ? '0' + now.date() : now.date())
date[i].list = []
for(let j = 0;j < 24; j++){
date[i].list?.push({
start:"",
num:""
})
}
}
console.log(date)
return {date, year, yue, day, zhou}
}