Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue317: Introducing Multiple Locale Support #318

Closed
wants to merge 3 commits into from
Closed
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 @@ -25,7 +25,7 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
export const REGEX_FORMAT = /\[.*?\]|Y{2,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{2,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}|(LTS|LT|LL?L?L?|l{1,4})|SSS/g

export const en = {
name: 'en',
Expand Down
32 changes: 32 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ class Dayjs {
const {
weekdays, months
} = locale

let { longDateFormat } = locale
if (longDateFormat === undefined) {
longDateFormat = {
LTS: 'h:mm:ss A',
LT: 'h:mm A',
L: 'MM/DD/YYYY',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY h:mm A',
LLLL: 'dddd, MMMM D, YYYY h:mm A'
}
}

const getShort = (arr, index, full, length) => (
(arr && arr[index]) || full[index].substr(0, length)
)
Expand Down Expand Up @@ -334,6 +347,25 @@ class Dayjs {
return Utils.padStart(this.$ms, 3, '0')
case 'Z':
return zoneStr

case 'LT':
case 'lt':
return this.format(longDateFormat.LT)
case 'LTS':
case 'lts':
return this.format(longDateFormat.LTS)
case 'L':
case 'l':
return this.format(longDateFormat.L)
case 'LL':
case 'll':
return this.format(longDateFormat.LL)
case 'LLL':
case 'lll':
return this.format(longDateFormat.LLL)
case 'LLLL':
case 'llll':
return this.format(longDateFormat.LLLL)
default: // 'ZZ'
return zoneStr.replace(':', '')
}
Expand Down
10 changes: 9 additions & 1 deletion src/locale/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ const locale = {
y: 'un año',
yy: '%d años'
},
ordinal: n => `${n}º`
ordinal: n => `${n}º`,
longDateFormat: {
LT: 'H:mm',
LTS: 'H:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D [de] MMMM [de] YYYY',
LLL: 'D [de] MMMM [de] YYYY H:mm',
LLLL: 'dddd, D [de] MMMM [de] YYYY H:mm'
}
}

dayjs.locale(locale, null, true)
Expand Down
10 changes: 9 additions & 1 deletion src/locale/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ const locale = {
y: 'un an',
yy: '%d ans'
},
ordinal: n => `${n}º`
ordinal: n => `${n}º`,
longDateFormat: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY HH:mm',
LLLL: 'dddd D MMMM YYYY HH:mm'
}
}

dayjs.locale(locale, null, true)
Expand Down
29 changes: 29 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import th from '../src/locale/th'
import '../src/locale/fr'
import '../src/locale/es'

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -114,6 +116,33 @@ it('Format Escaping characters', () => {
expect(dayjs().format(string)).toBe(moment().format(string))
})

it('Format LTS LT L LL LLL LLLL', () => {
expect(dayjs().format('LTS')).toBe(moment().format('LTS'))
expect(dayjs().format('LT')).toBe(moment().format('LT'))
expect(dayjs().format('L')).toBe(moment().format('L'))
expect(dayjs().format('LL')).toBe(moment().format('LL'))
expect(dayjs().format('LLL')).toBe(moment().format('LLL'))
expect(dayjs().format('LLLL')).toBe(moment().format('LLLL'))
})

it('French LTS, LT, L, LL, LLL, LLLL', () => {
expect(dayjs().locale('fr').format('LTS')).toBe(moment().locale('fr').format('LTS'))
expect(dayjs().locale('fr').format('LT')).toBe(moment().locale('fr').format('LT'))
expect(dayjs().locale('fr').format('L').toLowerCase()).toBe(moment().locale('fr').format('L'))
expect(dayjs().locale('fr').format('LL').toLowerCase()).toBe(moment().locale('fr').format('LL'))
expect(dayjs().locale('fr').format('LLL').toLowerCase()).toBe(moment().locale('fr').format('LLL'))
expect(dayjs().locale('fr').format('LLLL').toLowerCase()).toBe(moment().locale('fr').format('LLLL'))
})

it('Spanish LTS, LT, L, LL, LLL, LLLL', () => {
expect(dayjs().locale('es').format('LTS')).toBe(moment().locale('es').format('LTS'))
expect(dayjs().locale('es').format('LT')).toBe(moment().locale('es').format('LT'))
expect(dayjs().locale('es').format('L')).toBe(moment().locale('es').format('L'))
expect(dayjs().locale('es').format('LL').toLowerCase()).toBe(moment().locale('es').format('LL').toLowerCase())
expect(dayjs().locale('es').format('LLL').toLowerCase()).toBe(moment().locale('es').format('LLL').toLowerCase())
expect(dayjs().locale('es').format('LLLL').toLowerCase()).toBe(moment().locale('es').format('LLLL').toLowerCase())
})

describe('Difference', () => {
it('empty -> default milliseconds', () => {
const dateString = '20110101'
Expand Down