refactor(server): 日期/月份格式化统一改用 dayjs

- 替换散落的 toISOString().slice(0,10) / split('T')[0] / replace('T',' ')(UTC 语义用 dayjs(...).utc() 保持完全一致)
- Asia/Shanghai 固定时区日期(Intl.DateTimeFormat en-CA)改用 dayjs().utcOffset(8)
- 月份边界 first/last、daysInMonth、nextMonth、shiftDate/addDays 等改 dayjs 简洁实现
- 涉及 attendance/classes/classrooms/room/dashboard/bills/expenses/occupancies/sync/ai-chat/rental 等 28 个文件
This commit is contained in:
2026-08-08 17:52:32 +08:00
parent 92ba2e779f
commit 8936453e84
28 changed files with 82 additions and 112 deletions

View File

@@ -4,6 +4,7 @@ import { DataSource, Repository } from 'typeorm';
import { Bill, BillItem, RoomExpense, PersonalExpense, Occupancy, Room } from '../entities';
import { WalletsService } from '../wallets/wallets.service';
import type { GenerateBillsDto } from './dto/bill.dto';
import dayjs from '../common/dayjs';
@Injectable()
export class BillsGenerationService {
@@ -235,7 +236,7 @@ export class BillsGenerationService {
let year = startYear, month = startMonth;
year < endYear || (year === endYear && month <= endMonth);
) {
const daysInMonth = new Date(Date.UTC(year, month, 0)).getUTCDate();
const daysInMonth = dayjs.utc(`${year}-${month}`).daysInMonth();
const prefix = `${year}-${String(month).padStart(2, '0')}-`;
const overlapStart = activeStart > `${prefix}01` ? activeStart : `${prefix}01`;
const monthEnd = `${prefix}${String(daysInMonth).padStart(2, '0')}`;
@@ -258,7 +259,7 @@ export class BillsGenerationService {
private isValidDate(value: string) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(value || '')) return false;
const date = new Date(`${value}T00:00:00Z`);
return !Number.isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value;
return !Number.isNaN(date.getTime()) && dayjs(date).utc().format('YYYY-MM-DD') === value;
}