Customized Locale throws exception while formatting
See original GitHub issueDescribe 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.jsbased 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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

May be some easy way like this
I took a workd around. But
I faced this while implementing the custom locale.
If I want to extend the
enlocale just withrelativeTime: {}then the error happens i assume custome locale expects to declareweekdays,weekdaysShortand other related keys as mentioned in the below comment. https://github.com/iamkun/dayjs/issues/746#issuecomment-563927090This API dosent align with moment implementation. ref: https://runkit.com/embed/6abjer62rzj0
Can see err here : https://runkit.com/embed/32yqrafbgp6j