question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Customized Locale throws exception while formatting

See original GitHub issue

Describe the bug I am trying to convert a time stamp to a required format ‘LT’.

dayjs(1575872723701);

/* Returns this object
{
$D: 19
$H: 12
$L: undefined
$M: 10
$W: 2
$d: Tue Nov 19 2019 12:26:03 GMT+0530 (India Standard Time) {}
$m: 26
$ms: 136
$s: 3
$y: 2019
}
*/

and all the APIs like add, subtract, date works except format.

dayjs(1575872723701).format() 

returns Uncaught TypeError: Cannot read property ‘10’ of undefined

My usage

  • I have created a service file with all the plugin initializations of dayJs and importing it into the application
// services/dayjs.js
import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import relativeTime from 'dayjs/plugin/relativeTime';
import localizedFormat from 'dayjs/plugin/localizedFormat'

dayjs.extend(advancedFormat);
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat)

export default dayjs;

// somefile.js
import dayjs from './services/dayjs.js';

...
dayjs(1575872723701).format()  // throws error
  • I dont see any issue with other plugins and APIs except format.

  • I also set the locale of the /services/dayjs.js based on the locale sent. if not set to the below default config

import dayjs from './services/dayjs.js';

if(...){
const locale = {
      relativeTime: {
        name: 'en',
        future: '%s',
        past: '%s',
        s: function () {
          return 'now';
        },
        m: 'a minute ago',
        mm: '%d minutes ago',
        h: 'an hour',
        hh: '%d hours', // e.g. 2 hours, %d been replaced with 2
        d: 'a day',
        dd: '%d days',
        M: 'a month',
        MM: '%d months',
        y: 'a year',
        yy: '%d years'
      },
      ordinal: (n) => {
        const s = ['th', 'st', 'nd', 'rd']
        const v = n % 100
        return `[${n}${(s[(v - 20) % 10] || s[v] || s[0])}]`
      }
    }

    dayjs.locale(locale)
}

Would this be causing any error ?

Expected behavior Should ideally format to the provided option

Information

  • Day.js Version: 1.8.17
  • OS: MAC OS 10.15.1
  • Browser: Chrome Version 78.0.3904.108 and all major browsers
  • Time zone: GMT + 5.30

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
iamkuncommented, Dec 11, 2019

May be some easy way like this

dayjs.Ls['en'].weekdays = ['a', 'b','c','d','e','f','g'] // update locale 
dayjs.locale(dayjs.Ls['en'])
console.log(dayjs().format('dddd'))
0reactions
SarathSantoshDamarajucommented, Dec 11, 2019

I took a workd around. But

I faced this while implementing the custom locale.

If I want to extend the en locale just with relativeTime: {} then the error happens i assume custome locale expects to declare weekdays, weekdaysShort and other related keys as mentioned in the below comment. https://github.com/iamkun/dayjs/issues/746#issuecomment-563927090

This API dosent align with moment implementation. ref: https://runkit.com/embed/6abjer62rzj0

Can see err here : https://runkit.com/embed/32yqrafbgp6j

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dayjs custom locale throws Formatting exceptions
I want to create a custom locale and use it with DayJs. and the procedure is mentioned here in docs. But, when I...
Read more >
Locale independent error handling - Medium
On throwing an exception some diagnostic data already in place: ... In case the error message should be translated to human readable format...
Read more >
Formatter (Java SE 16 & JDK 16) - Oracle Help Center
An interpreter for printf-style format strings. This class provides support for layout justification and alignment, common formats for numeric, string, ...
Read more >
SimpleDateFormat - Android Developers
The format is locale independent and digits must be taken from the Basic ... IllegalArgumentException is thrown when constructing a SimpleDateFormat or ...
Read more >
String.Format Method (System) - Microsoft Learn
The Format method throws a FormatException exception if the index of an index item is greater than or equal to the number of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found