diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 32b903e2..310bceab 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -124,6 +124,26 @@ dayjs.extend(isLeapYear) dayjs('2000-01-01').isLeapYear(); // true ``` +### BuddhistEra +- BuddhistEra extends `dayjs().format` API to supply Buddhist Era (B.E.) format options. +- Buddhist Era is a year numbering system that primarily used in mainland Southeast Asian countries of Cambodia, Laos, Myanmar and Thailand as well as in Sri Lanka and Chinese populations of Malaysia and Singapore for religious or official occasions ([Wikipedia](https://en.wikipedia.org/wiki/Buddhist_calendar)) +- To calculate BE year manually, just add 543 to year. For example 26 May 1977 AD/CE should display as 26 May 2520 BE (1977 + 543) + +```javascript +import buddhistEra from 'dayjs/plugin/buddhistEra' + +dayjs.extend(buddhistEra) + +dayjs().format('BBBB BB') +``` + +List of added formats: + +| Format | Output | Description | +| ------ | ---------------- | ------------------------------------- | +| `BBBB` | 2561 | Full BE Year (Year + 543) | +| `BB` | 61 | 2-digit of BE Year | + ## Customize You could build your own Day.js plugin to meet different needs. diff --git a/src/plugin/buddhistEra/index.js b/src/plugin/buddhistEra/index.js new file mode 100644 index 00000000..8f878e43 --- /dev/null +++ b/src/plugin/buddhistEra/index.js @@ -0,0 +1,22 @@ +import { FORMAT_DEFAULT } from '../../constant' + +export default (o, c) => { // locale needed later + const proto = c.prototype + const oldFormat = proto.format + // extend en locale here + proto.format = function (formatStr, localeObject) { + const yearBias = 543 + const utils = this.$utils() + const locale = localeObject || this.$locale() + const str = formatStr || FORMAT_DEFAULT + const result = str.replace(/BBBB|BB/g, (match) => { + switch (match) { + case 'BB': + return utils.padStart(String(this.$y + yearBias).slice(-2), 2, '0') + default: // BBBB + return utils.padStart(String(this.$y + yearBias), 4, '0') + } + }) + return oldFormat.bind(this)(result, locale) + } +} diff --git a/test/plugin/buddhistEra.test.js b/test/plugin/buddhistEra.test.js new file mode 100644 index 00000000..2482702f --- /dev/null +++ b/test/plugin/buddhistEra.test.js @@ -0,0 +1,33 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import buddhistEra from '../../src/plugin/buddhistEra' + +dayjs.extend(buddhistEra) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('Format empty string', () => { + expect(dayjs().format()).toBe(moment().format()) +}) + +it('Format Buddhist Era 2 digit', () => { + expect(dayjs().format('BB')).toBe(`${(moment().year() + 543) % 100}`) +}) + +it('Format Buddhist Era 4 digit', () => { + expect(dayjs().format('BBBB')).toBe(`${moment().year() + 543}`) +}) + +it('Format Buddhist Era 4 digit with other format', () => { + const format = 'D MMM BBBB' + const today = moment() + const momentDate = today.format(format).replace('BBBB', today.year() + 543) + expect(dayjs().format(format)).toBe(momentDate) +})