DateUtility
Tong quan
DateUtility cung cap instance Day.js duoc cau hinh san voi cac plugin thiet yeu de thao tac ngay thang nhan biet mui gio. Tat ca dich vu backend deu import dayjs tu @nx/core de dam bao xu ly ngay thang nhat quan tren toan he thong.
Nguon: packages/core/src/utilities/date.utility.ts (23 dong)
Nguon
Toan bo module gom 23 dong -- mot singleton export duoc cau hinh san:
typescript
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import isoWeek from 'dayjs/plugin/isoWeek';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
dayjs.extend(customParseFormat);
dayjs.extend(timezone);
dayjs.extend(isoWeek);
dayjs.extend(utc);
dayjs.extend(isSameOrBefore);
dayjs.extend(isSameOrAfter);
const tz = process.env.APP_ENV_APPLICATION_TIMEZONE ?? 'Asia/Ho_Chi_Minh';
dayjs.tz.setDefault(tz);
export { dayjs };Cau hinh
Mui gio Mac dinh
Mui gio mac dinh la Asia/Ho_Chi_Minh (UTC+7). Co the ghi de thong qua bien moi truong APP_ENV_APPLICATION_TIMEZONE.
| Bien | Bat buoc | Mac dinh | Mo ta |
|---|---|---|---|
APP_ENV_APPLICATION_TIMEZONE | Khong | Asia/Ho_Chi_Minh | Dinh danh mui gio IANA |
bash
# .env.development
APP_ENV_APPLICATION_TIMEZONE=Asia/Ho_Chi_Minh # Default
APP_ENV_APPLICATION_TIMEZONE=America/New_York # Override for US Eastern
APP_ENV_APPLICATION_TIMEZONE=UTC # UTC modeCac Plugin Da tai
| Plugin | Muc dich | Vi du |
|---|---|---|
customParseFormat | Phan tich ngay voi chuoi dinh dang tuy chinh | dayjs('20/01/2025', 'DD/MM/YYYY') |
timezone | Chuyen doi va nhan biet mui gio | dayjs().tz('Asia/Tokyo') |
isoWeek | Tinh toan so tuan ISO | dayjs().isoWeek() |
utc | Thao tac che do UTC | dayjs.utc() |
isSameOrBefore | So sanh "truoc" bao gom | date.isSameOrBefore(other) |
isSameOrAfter | So sanh "sau" bao gom | date.isSameOrAfter(other) |
Import
typescript
import { dayjs } from '@nx/core';
// or
import { dayjs } from '@nx/core/utilities';Cach Su dung Co ban
Ngay/Gio Hien tai
typescript
// Current time in default timezone (Asia/Ho_Chi_Minh)
const now = dayjs();
// Current time in UTC
const utcNow = dayjs.utc();
// Current time in a specific timezone
const tokyoNow = dayjs().tz('Asia/Tokyo');Phan tich Ngay
typescript
// ISO string
const date1 = dayjs('2025-01-20T10:00:00Z');
// Custom format (requires customParseFormat plugin)
const date2 = dayjs('20/01/2025', 'DD/MM/YYYY');
// From timestamp
const date3 = dayjs(1705708800000);
// From Date object
const date4 = dayjs(new Date());Dinh dang Ngay
typescript
const date = dayjs('2025-01-20T15:30:00');
date.format('YYYY-MM-DD'); // "2025-01-20"
date.format('DD/MM/YYYY'); // "20/01/2025"
date.format('YYYY-MM-DD HH:mm:ss'); // "2025-01-20 15:30:00"
date.format('YYYY-MM-DD HH:mm:ss Z'); // "2025-01-20 15:30:00 +07:00"Thao tac Mui gio
Chuyen doi Giua cac Mui gio
typescript
const date = dayjs('2025-01-20T10:00:00Z'); // UTC
// Convert to Vietnam time
const vnTime = date.tz('Asia/Ho_Chi_Minh');
vnTime.format('HH:mm'); // "17:00"
// Convert to US Eastern
const etTime = date.tz('America/New_York');
etTime.format('HH:mm'); // "05:00"Mau Luu tru va Hien thi
typescript
// Store in UTC (for database)
const stored = dayjs().utc().toISOString();
// "2025-01-20T08:30:00.000Z"
// Display in user's timezone
const displayTime = dayjs(stored).tz('Asia/Ho_Chi_Minh').format('HH:mm DD/MM/YYYY');
// "15:30 20/01/2025"Luong Mui gio
So sanh Ngay
So sanh Co ban
typescript
const date1 = dayjs('2025-01-20');
const date2 = dayjs('2025-01-25');
date1.isBefore(date2); // true
date1.isAfter(date2); // false
date1.isSame(date2); // false
date1.isSame(date2, 'month'); // true (same month)Cung hoac Truoc/Sau
Cac phuong thuc nay den tu plugin isSameOrBefore va isSameOrAfter:
typescript
const startDate = dayjs('2025-01-01');
const endDate = dayjs('2025-01-31');
const checkDate = dayjs('2025-01-15');
// Check if within an inclusive range
const isInRange =
checkDate.isSameOrAfter(startDate) &&
checkDate.isSameOrBefore(endDate);
// trueSo sanh voi Do chi tiet
typescript
const date1 = dayjs('2025-01-20 10:00');
const date2 = dayjs('2025-01-20 15:00');
date1.isSame(date2, 'day'); // true (same day)
date1.isSame(date2, 'hour'); // false (different hour)
date1.isBefore(date2, 'hour'); // trueThao tac Ngay
Them/Tru
typescript
const date = dayjs('2025-01-20');
date.add(7, 'day').format('YYYY-MM-DD'); // "2025-01-27"
date.add(1, 'month').format('YYYY-MM-DD'); // "2025-02-20"
date.subtract(1, 'year').format('YYYY-MM-DD'); // "2024-01-20"
// Chaining
date.add(1, 'month').add(15, 'day').format('YYYY-MM-DD');
// "2025-03-07"Bat dau/Ket thuc Ky
typescript
const date = dayjs('2025-01-20 15:30:45');
date.startOf('day').format('YYYY-MM-DD HH:mm:ss');
// "2025-01-20 00:00:00"
date.endOf('day').format('YYYY-MM-DD HH:mm:ss');
// "2025-01-20 23:59:59"
date.startOf('month').format('YYYY-MM-DD');
// "2025-01-01"
date.endOf('month').format('YYYY-MM-DD');
// "2025-01-31"Thao tac Tuan ISO
Plugin isoWeek cung cap tinh toan tuan ISO 8601:
typescript
const date = dayjs('2025-01-20');
date.isoWeek(); // 4 (ISO week number)
date.isoWeekday(); // 1 (Monday = 1, Sunday = 7)
date.isoWeekYear(); // 2025
// Get start of ISO week (Monday)
const weekStart = date.startOf('isoWeek');
weekStart.format('YYYY-MM-DD'); // "2025-01-20" (Monday)
// Get end of ISO week (Sunday)
const weekEnd = date.endOf('isoWeek');
weekEnd.format('YYYY-MM-DD'); // "2025-01-26" (Sunday)Truong hop Su dung Pho bien
Xac thuc Ngay Su kien
typescript
function isEventDateValid(eventDate: string): boolean {
const event = dayjs(eventDate);
const now = dayjs();
const maxFutureDate = now.add(1, 'year');
return event.isAfter(now) && event.isBefore(maxFutureDate);
}Khoang Ngay cho Bao cao
typescript
function getMonthlyReportRange(year: number, month: number) {
const start = dayjs()
.year(year)
.month(month - 1)
.startOf('month');
const end = start.endOf('month');
return {
start: start.toISOString(),
end: end.toISOString(),
};
}Tich hop Co so Du lieu
typescript
// Store in UTC
async createEvent(data: CreateEventDto) {
const eventDateUtc = dayjs
.tz(data.eventDate, data.timezone)
.utc()
.toISOString();
return this.eventRepository.create({
data: {
...data,
eventDate: eventDateUtc,
},
});
}
// Query by date range
const weekStart = dayjs().startOf('isoWeek').toISOString();
const weekEnd = dayjs().endOf('isoWeek').toISOString();
const events = await eventRepository.find({
where: {
eventDate: { gte: weekStart, lte: weekEnd },
},
});Thuc hanh Tot nhat
1. Luon Luu tru UTC
typescript
// Correct -- store in UTC
const stored = dayjs.tz(userInput, userTimezone).utc().toISOString();
// Avoid -- timezone-ambiguous
const stored = dayjs(userInput).format();2. Phan tich voi Mui gio Ro rang
typescript
// Correct -- explicit timezone
const date = dayjs.tz('2025-01-20 15:00', 'Asia/Ho_Chi_Minh');
// Avoid -- ambiguous
const date = dayjs('2025-01-20 15:00'); // Which timezone?3. Su dung Dinh dang ISO cho Phan hoi API
typescript
// Correct -- machine-readable
{ createdAt: dayjs(record.createdAt).toISOString() }
// "2025-01-20T08:30:00.000Z"
// Avoid -- locale-specific
{ createdAt: dayjs(record.createdAt).format('DD/MM/YYYY') }
// "20/01/2025"Tham chieu IGNIS Framework
Instance dayjs duoc export boi @nx/core xay dung tren tien ich Date cua IGNIS. Tai lieu tien ich co so, xem: