Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export const INVALID_DATE_STRING = 'Invalid Date'

// regex
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|S{1,3}/g
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ class Dayjs {
return String(this.$s)
case 'ss':
return Utils.s(this.$s, 2, '0')
case 'S':
return String(Math.round(this.$ms / 100))
case 'SS':
return Utils.s(Math.round(this.$ms / 10), 2, '0')
case 'SSS':
return Utils.s(this.$ms, 3, '0')
case 'Z':
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { u } from '../localizedFormat/utils'

const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|SS?S?|z|ZZ?)/g

const match1 = /\d/ // 0 - 9
const match2 = /\d\d/ // 00 - 99
Expand Down
2 changes: 2 additions & 0 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class Duration {
mm: $u.s(this.$d.minutes, 2, '0'),
s: this.$d.seconds,
ss: $u.s(this.$d.seconds, 2, '0'),
S: String(Math.round(this.$d.milliseconds / 100)),
SS: $u.s(Math.round(this.$d.milliseconds / 10), 2, '0'),
SSS: $u.s(this.$d.milliseconds, 3, '0')
}
return str.replace(REGEX_FORMAT, (match, $1) => $1 || String(matches[match]))
Expand Down
18 changes: 18 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,21 @@ it('parse w, ww', () => {
const format2 = 'YYYY-[w]ww'
expect(dayjs(input2, format2).format(format1)).toBe(input2)
})

describe('parse milliseconds', () => {
it('S', () => {
const input = '2024-01-01T00:00:00.1'
const format1 = 'YYYY-MM-DDTHH:mm:ss.S'
expect(dayjs(input, format1, true).format(format1)).toBe(input)
})
it('SS', () => {
const input = '2024-01-01T00:00:00.12'
const format1 = 'YYYY-MM-DDTHH:mm:ss.SS'
expect(dayjs(input, format1, true).format(format1)).toBe(input)
})
it('SSS', () => {
const input = '2024-01-01T00:00:00.123'
const format1 = 'YYYY-MM-DDTHH:mm:ss.SSS'
expect(dayjs(input, format1, true).format(format1)).toBe(input)
})
})
2 changes: 1 addition & 1 deletion test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe('Format', () => {
.add(9, 'months')
.add(2, 'years')
.add(10, 'milliseconds')
expect(d.format('Y/YY.YYYYTESTM:MM:D:DD:H:HH:m:mm:s:ss:SSS'))
expect(d.format('Y/YY.YYYY[TEST]M:MM:D:DD:H:HH:m:mm:s:ss:SSS'))
.toBe('2/02.0002TEST9:09:6:06:8:08:5:05:1:01:010')
})
})