Skip to content
Day.js

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

Appearance

Sidebar Navigation

Installation

Node.js

Browser

TypeScript

Download

Parse

Now

String

String + Format

Unix Timestamp (milliseconds)

Unix Timestamp (seconds)

Date

Object

Array

UTC

Dayjs Clone

Validation

Get + Set

Millisecond

Second

Minute

Hour

Date of Month

Day of Week

Day of Week (Locale Aware)

ISO Day of Week

Day of Year

Week of Year

Week of Year (ISO)

Month

Quarter

Year

Week Year

Week Year (ISO)

Weeks In Year (ISO)

Get

Set

Maximum

Minimum

Manipulate

Add

Subtract

Start of Time

End of Time

Local

UTC

UTC offset

Display

Format

Time from now

Time from X

Time to now

Time to X

Calendar Time

Difference

Unix Timestamp (milliseconds)

Unix Timestamp

Days in Month

As Javascript Date

As Array

As JSON

As ISO 8601 String

As Object

As String

Query

Is Before

Is Same

Is After

Is Same or Before

Is Same or After

Is Between

Is a Dayjs

Is Leap Year

i18n

Loading locale in NodeJS

Loading locale in the browser

Changing locale globally

Changing locales locally

Checking the current Day.js locale

Listing the months and weekdays of the current locale

Accessing locale specific functionality

Plugins

Loading plugin in NodeJS

Loading plugin in the browser

AdvancedFormat

ArraySupport

BadMutable

BigIntSupport

BuddhistEra

Calendar

CustomParseFormat

DayOfYear

DevHelper

Duration

IsBetween

IsLeapYear

IsSameOrAfter

IsSameOrBefore

IsToday

IsTomorrow

IsYesterday

IsoWeek

IsoWeeksInYear

LocaleData

LocalizedFormat

MinMax

ObjectSupport

PluralGetSet

PreParsePostFormat

QuarterOfYear

RelativeTime

Timezone

ToArray

ToObject

UpdateLocale

UTC

weekOfYear

WeekYear

Weekday

Customize

Month Names

Month Abbreviations

Weekday Names

Weekday Abbreviations

Minimal Weekday Abbreviations

Relative Time

Calendar

Durations

Creating

Clone

Humanize

Format

Milliseconds

Seconds

Minutes

Hours

Days

Weeks

Months

Years

Add Time

Subtract Time

Using Duration with Diff

As Unit of Time

Get Unit of Time

As JSON

Is a Duration

As ISO 8601 String

Locale

Time Zone

Parsing in Zone

Converting to Zone

Guessing user zone

Set Default Timezone

On this page

PreParsePostFormat ​

Pre-parse / Post-format lets you process the input before the parser and process the string output after the formatter. Based on similar behavior for locales in moment.js.

NOTE: this plugin requires the localeData plugin to be imported before it (as it depends on it's functionality).

NOTE: this plugin also affects the relative time plugin, also by design (mimics the moment.js implementaiton behavior).

Sample usage ​

e.g. In the AR locale specifically, it is used to support Arabic numerals.

javascript
// Arabic [ar]
import dayjs from 'dayjs';
import preParsePostFormat from 'dayjs/plugin/preParsePostFormat';
dayjs.extend(preParsePostFormat);

const months =
  'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split(
    '_'
  );
const symbolMap = {
  1: '١',
  2: '٢',
  3: '٣',
  4: '٤',
  5: '٥',
  6: '٦',
  7: '٧',
  8: '٨',
  9: '٩',
  0: '٠',
};

const numberMap = {
  '١': '1',
  '٢': '2',
  '٣': '3',
  '٤': '4',
  '٥': '5',
  '٦': '6',
  '٧': '7',
  '٨': '8',
  '٩': '9',
  '٠': '0',
};

const locale = {
  name: 'ar',
  // ...
  preparse(string) {
    return string
      .replace(/[١٢٣٤٥٦٧٨٩٠]/g, match => numberMap[match])
      .replace(/،/g, ',');
  },
  postformat(string) {
    return string.replace(/\d/g, match => symbolMap[match]).replace(/,/g, '،');
  },
  // ...
};
// ...

The tests should also give you a good idea on how to use the plugin if this isn't clear enough 😉.

Pager
Previous pagePluralGetSet
Next pageQuarterOfYear

Released under the MIT License.

Copyright (c) 2020 iamkun

https://day.js.org/docs/en/plugin/preparse-postformat

Released under the MIT License.

Copyright (c) 2020 iamkun