@traders/dayjs
v1.0.0
Published
```ts /* eslint-disable ts/ban-ts-comment */ import mockdate from 'mockdate' import { afterEach } from 'node:test' import { beforeEach, expect, it } from 'vitest' import { dayjs } from '.'
Downloads
19
Maintainers
Readme
Usage
/* eslint-disable ts/ban-ts-comment */
import mockdate from 'mockdate'
import { afterEach } from 'node:test'
import { beforeEach, expect, it } from 'vitest'
import { dayjs } from '.'
const utcYYYYMMDD = `2024-01-01`
let utcNow = dayjs(`${utcYYYYMMDD}T00:00:00Z`)
const utcUnix10 = 1704067200
const utcUnix13 = 1704067200000
beforeEach(() => {
utcNow = dayjs(`${utcYYYYMMDD}T00:00:00Z`)
})
afterEach(() => {
mockdate.reset()
})
it('空參數一樣取得的是以 +8 為基準', () => {
mockdate.set(utcUnix13)
const now = dayjs()
expect(now.formatDateTimeSeconds()).toBe(utcNow.formatDateTimeSeconds())
})
it('.getTimestamp() 來取得 10位數 或者 13位數的 timestamp', () => {
expect(utcNow.toDate().getTime()).toBe(utcUnix13)
expect(utcNow.time()).toBe(utcUnix13)
expect(utcNow.unix()).toBe(utcUnix10)
expect(utcNow.getTimestamp().seconds).toBe(utcUnix10)
expect(utcNow.getTimestamp().milliseconds).toBe(utcUnix13)
})
it('.format**()', () => {
expect(utcNow.formatDate()).toMatch(`${utcYYYYMMDD.replaceAll('-', '/')}`)
expect(utcNow.formatDateTime()).toMatch(`${utcYYYYMMDD.replaceAll('-', '/')} 00:00`)
expect(utcNow.formatDateTimeSeconds()).toMatch(`${utcYYYYMMDD.replaceAll('-', '/')} 00:00:00`)
})
it('.setTime(:any)', () => {
expect(utcNow.setTime('i dont care').format()).toMatch(`Invalid Date`)
// @ts-expect-error
expect(utcNow.setTime(() => {}).format()).toMatch(`Invalid Date`)
})
it('.setTime(:number)', () => {
expect(utcNow.setTime(-1).format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
expect(utcNow.setTime(0).format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
expect(utcNow.setTime(6.15).format()).toMatch(`${utcYYYYMMDD}T06:15:00`)
expect(utcNow.setTime(6.3).format()).toMatch(`${utcYYYYMMDD}T06:30:00`)
expect(utcNow.setTime(6.45).format()).toMatch(`${utcYYYYMMDD}T06:45:00`)
expect(utcNow.setTime(6.5).format()).toMatch(`${utcYYYYMMDD}T06:50:00`)
expect(utcNow.setTime(6.59).format()).toMatch(`${utcYYYYMMDD}T06:59:00`)
expect(utcNow.setTime(6.77).format()).toMatch(`${utcYYYYMMDD}T06:59:59`)
expect(utcNow.setTime(23.59).format()).toMatch(`${utcYYYYMMDD}T23:59:00`)
expect(utcNow.setTime(23.5959).format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
expect(utcNow.setTime(24).format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
})
it('.setTime(:string)', () => {
expect(utcNow.setTime('i am invalid string').format()).toBe(`Invalid Date`)
//
// 冒號
expect(utcNow.setTime('-1').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
expect(utcNow.setTime('0').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
expect(utcNow.setTime('6:15').format()).toMatch(`${utcYYYYMMDD}T06:15:00`)
expect(utcNow.setTime('6:3').format()).toMatch(`${utcYYYYMMDD}T06:30:00`)
expect(utcNow.setTime('6:45').format()).toMatch(`${utcYYYYMMDD}T06:45:00`)
expect(utcNow.setTime('6:5').format()).toMatch(`${utcYYYYMMDD}T06:50:00`)
expect(utcNow.setTime('6:59').format()).toMatch(`${utcYYYYMMDD}T06:59:00`)
expect(utcNow.setTime('6:77').format()).toMatch(`${utcYYYYMMDD}T06:59:59`)
expect(utcNow.setTime('23:59').format()).toMatch(`${utcYYYYMMDD}T23:59:00`)
expect(utcNow.setTime('23:5959').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
expect(utcNow.setTime('24').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
//
// 小數點也可以
expect(utcNow.setTime('-1').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
expect(utcNow.setTime('0').format()).toMatch(`${utcYYYYMMDD}T00:00:00`)
expect(utcNow.setTime('6.15').format()).toMatch(`${utcYYYYMMDD}T06:15:00`)
expect(utcNow.setTime('6.3').format()).toMatch(`${utcYYYYMMDD}T06:30:00`)
expect(utcNow.setTime('6.45').format()).toMatch(`${utcYYYYMMDD}T06:45:00`)
expect(utcNow.setTime('6.5').format()).toMatch(`${utcYYYYMMDD}T06:50:00`)
expect(utcNow.setTime('6.59').format()).toMatch(`${utcYYYYMMDD}T06:59:00`)
expect(utcNow.setTime('6.77').format()).toMatch(`${utcYYYYMMDD}T06:59:59`)
expect(utcNow.setTime('23.59').format()).toMatch(`${utcYYYYMMDD}T23:59:00`)
expect(utcNow.setTime('23.5959').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
expect(utcNow.setTime('24').format()).toMatch(`${utcYYYYMMDD}T23:59:59`)
})
it('dayjs(:number) 接受參數為 10位數字(處理誤植) 或 13位數字 或 16位數字(處理誤植)', () => {
const unix10 = 1704067200
const toBeTimeString = `2024/01/01 00:00:00`
expect(dayjs(unix10).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(unix10 * 1000).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(unix10 * 1000 * 1000).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024-01-01T00:00:00Z`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024-01-01 00:00:00`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024-01-01`).formatDateTimeSeconds()).toBe(toBeTimeString)
})
it('dayjs(:string) 接受參數為 10位數字(處理誤植) 或 13位數字 或 16位數字(處理誤植)', () => {
const utcInputUnix10 = 1704067200
const toBeTimeString = `2024/01/01 00:00:00`
expect(dayjs(`${utcInputUnix10}`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`${utcInputUnix10 * 1000}`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`${utcInputUnix10 * 1000 * 1000}`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024-01-01T00:00:00Z`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024/01/01 00:00:00`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024-01-01 00:00:00`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`2024-01-01`).formatDateTimeSeconds()).toBe(toBeTimeString)
})
it('dayjs(:string) 接受參數為 含有下劃線、或者 e 符號的字串型態的數字', () => {
const toBeTimeString = `2024/01/01 00:00:00`
expect(dayjs(`1704067200`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`1704067200_000`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`1704067200_000_000`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`1704067200e3`).formatDateTimeSeconds()).toBe(toBeTimeString)
expect(dayjs(`1704067200e6`).formatDateTimeSeconds()).toBe(toBeTimeString)
})