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

Default calendar formats #805

Closed
scorsi opened this issue Feb 27, 2020 · 5 comments
Closed

Default calendar formats #805

scorsi opened this issue Feb 27, 2020 · 5 comments

Comments

@scorsi
Copy link

scorsi commented Feb 27, 2020

Hello,

I'm looking to change the default formats of the Calendar plugins.
Actually, I'm forced to override the calendar method like this:

const oldCalendar = dayjs.prototype.calendar;
dayjs.prototype.calendar = function newCalendar(referenceTime = null, formats = {}) {
  return oldCalendar.bind(this)(referenceTime, {
    // TODO: add i18n here
    sameDay: '[Aujourd\'hui à] LT', 
    lastDay: '[Hier à] LT',
    lastWeek: 'dddd [dernier à] LT',
    sameElse: 'LLLL',
    ...formats,
  });
};

Which does not respect our code standards.

Is there any other options ?

Thanks,

@iamkun
Copy link
Owner

iamkun commented Feb 29, 2020

You can also pass specifying calendar output formats as the second parameter.

https://day.js.org/docs/en/display/calendar-time

@scorsi
Copy link
Author

scorsi commented Feb 29, 2020

Well, actually I have to pass the exact same calendar format to a lot a different place in my code. To avoid code duplication I would like to setup a default formats to dayjs for calendar plugin so, I just have to call mydate.calendar() which is what I do with momentjs.

@iamkun
Copy link
Owner

iamkun commented Feb 29, 2020

Oh, I see. Maybe there's a better way. You can update the locale config like this.

var dayjs = require("dayjs")
var calendar = require('dayjs/plugin/calendar')
var updateLocale = require('dayjs/plugin/updateLocale')
dayjs.extend(updateLocale)
dayjs.extend(calendar)
dayjs.updateLocale('en', {
  calendar: {
  sameDay: '[Today at] h:mm A[ your format]',
  nextDay: '[Tomorrow][ your format]', 
  nextWeek: 'dddd[ your format]', 
  lastDay: '[Yesterday][ your format]',
  lastWeek: '[Last] dddd[ your format]', 
  sameElse: 'DD/MM/YYYY[ your format]'
}
})

dayjs().calendar(dayjs('2008-01-01'))

https://runkit.com/embed/w2atmrrgv3kb
Screen Shot 2020-02-29 at 5 25 31 PM

@scorsi
Copy link
Author

scorsi commented Feb 29, 2020

Awesome, I’ll try that but it’s appear to be what I would like to achieve and in addition with i18n spec ! Thanks !

@iamkun
Copy link
Owner

iamkun commented Apr 17, 2020

fixed

@iamkun iamkun closed this as completed Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants