Last active
March 10, 2020 09:41
-
-
Save terence410/d26c45f08e912998d45db5a15be381ef to your computer and use it in GitHub Desktop.
Day JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://day.js.org/docs/en/display/format | |
import dayjs, {OpUnitType} from "dayjs"; | |
import relativeTime from "dayjs/plugin/relativeTime"; | |
import utc from "dayjs/plugin/utc"; | |
import weekOfYear from "dayjs/plugin/weekOfYear"; | |
dayjs.extend(utc); | |
dayjs.extend(weekOfYear); | |
dayjs.extend(relativeTime); | |
import {configs} from "../../configs"; | |
type IDateType = dayjs.Dayjs | string | number | Date; | |
type IOptions = IDateType | { | |
date?: IDateType, | |
addWeeks?: number, | |
addDays?: number, | |
addHours?: number, | |
addMinutes?: number, | |
addSeconds?: number, | |
startOf?: OpUnitType; | |
endOf?: OpUnitType; | |
}; | |
export function getDayJs(): dayjs.Dayjs { | |
return dayjs(); | |
} | |
export function getLocalDate(options: IOptions = {}): Date { | |
return getLocalDayJs(options).toDate(); | |
} | |
export function getLocalTime(options: IOptions = {}): number { | |
return getLocalDayJs(options).toDate().getTime(); | |
} | |
export function getLocalDayJs(options: IOptions = {}): dayjs.Dayjs { | |
if (options instanceof Date || typeof options === "string" || typeof options === "number" || dayjs.isDayjs(options)) { | |
options = {date: options}; | |
} | |
let dayJsOb = dayjs(options.date).utcOffset(configs.utcOffset); | |
if (options.addWeeks) { | |
dayJsOb = dayJsOb.add(options.addWeeks, "week"); | |
} | |
if (options.addDays) { | |
dayJsOb = dayJsOb.add(options.addDays, "day"); | |
} | |
if (options.addHours) { | |
dayJsOb = dayJsOb.add(options.addHours, "hour"); | |
} | |
if (options.addMinutes) { | |
dayJsOb = dayJsOb.add(options.addMinutes, "minute"); | |
} | |
if (options.addSeconds) { | |
dayJsOb = dayJsOb.add(options.addSeconds, "second"); | |
} | |
if (options.startOf) { | |
dayJsOb = dayJsOb.startOf(options.startOf); | |
} | |
if (options.endOf) { | |
dayJsOb = dayJsOb.endOf(options.endOf); | |
} | |
return dayJsOb; | |
} | |
export function formatLocal(template: string, options: IOptions = {}) { | |
return getLocalDayJs(options).format(template); | |
} | |
export function formatLocalDate(options: IOptions = {}) { | |
return getLocalDayJs(options).format("YYYY-MM-DD"); | |
} | |
export function formatLocalDateTime(options: IOptions = {}) { | |
return getLocalDayJs(options).format("YYYY-MM-DD HH:mm"); | |
} | |
export function formatLocalMonthWeek(options: IOptions) { | |
const dayJsObj = getLocalDayJs(options); | |
const date1 = dayJsObj.week(); | |
const date2 = dayJsObj.clone().startOf("month").week(); | |
let week = date1 - date2 + 1; | |
if (week <= 0) { | |
week = 1; | |
} | |
const month = dayJsObj.format("YYYY MMM"); | |
return `${month} (W${week})`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment