-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Calendar format returns wrong date #1226
Comments
I had the same issue. As a workaround, I test for diff < 6 days if (
Math.abs(
dayjs().diff(file.lastModified, "day")
) < 6
) {
console.log(
"lm1",
dayjs().calendar(dayjs(file.lastModified))
);
} else {
console.log(
"lm2",
dayjs(file.lastModified).format(
"YYYY-MM-DD"
)
);
} |
Here is a more complete workaround that overrides calendar() with the fix: import "dayjs/locale/fr";
import dayjs from "dayjs";
import calendar from "dayjs/plugin/calendar";
import updateLocale from "dayjs/plugin/updateLocale";
dayjs.extend(calendar);
dayjs.extend(updateLocale);
dayjs.updateLocale("fr", {
calendar: {
sameDay: "[aujourd'hui à] H:mm",
nextDay: "[demain à] H:mm",
nextWeek: "dddd [prochain]",
lastDay: "[hier à] H:mm",
lastWeek: "dddd [dernier]",
sameElse: "YYYY-MM-DD",
},
});
const defaultCalendar = dayjs.prototype.calendar;
dayjs.prototype.calendar = function calendar(
referenceTime?: string | number | dayjs.Dayjs | Date | undefined,
formats?: object | undefined
) {
if (
dayjs().diff(dayjs(referenceTime), "day") < -6 ||
dayjs().diff(dayjs(referenceTime), "day") > 7
) {
return dayjs(referenceTime).format("YYYY-MM-DD");
} else {
return defaultCalendar.call(dayjs(referenceTime), [dayjs(), formats]);
}
};
export default dayjs; |
We could even extend that more to take into account whether or not our dates belong to the same week, which seems more accurate.
So we would need to add a export default (o, c, d) => {
const LT = 'h:mm A'
const L = 'MM/DD/YYYY'
const calendarFormat = {
lastDay: `[Yesterday at] ${LT}`,
sameDay: `[Today at] ${LT}`,
nextDay: `[Tomorrow at] ${LT}`,
sameWeek: `dddd [at] ${LT}`,
nextWeek: `[Next] dddd [at] ${LT}`,
lastWeek: `[Last] dddd [at] ${LT}`,
sameElse: L
}
const proto = c.prototype
proto.calendar = function (referenceTime, formats) {
const format = formats || this.$locale().calendar || calendarFormat
const referenceStartOfDay = d(referenceTime || undefined).startOf('d')
const diff = this.diff(referenceStartOfDay, 'd', true)
const weekDiff = this.week() - referenceStartOfDay.week()
const sameElse = 'sameElse'
/* eslint-disable no-nested-ternary */
const retVal = weekDiff < -1 || weekDiff > 1 ? sameElse :
diff < -1 ? weekDiff === 0 ? 'sameWeek' : 'lastWeek' :
diff < 0 ? 'lastDay' :
diff < 1 ? 'sameDay' :
diff < 2 ? 'nextDay' :
weekDiff === 0 ? 'sameWeek' : 'nextWeek'
/* eslint-enable no-nested-ternary */
const currentFormat = format[retVal] || calendarFormat[retVal]
if (typeof currentFormat === 'function') {
return currentFormat.call(this, d())
}
return this.format(currentFormat)
}
} |
Any update on this? I'm patching the plugin for now similar to what @borie88 helpfully suggested above. Just curious if this will make it into the library. Here's a reproduction if anyone else encountering this issue wants to look: https://codesandbox.io/s/billowing-dream-7obed |
I solved this issue by switching the two dates : // wrong
dayjs().calendar(dayjs(date))
// good
dayjs(date).calendar(dayjs())
// better!
dayjs(date).calendar() I don't know if the issue is about the documentation or the calendar plugin. |
dayjs.extend(calendar)
dayjs(date).calendar(undefined, {
sameDay: '[Today at] h:mm A',
lastDay: '[Yesterday at] h:mm A',
lastWeek: '[Last] dddd [at] h:mm A',
sameElse: 'DD/MM/YYYY'
}) That's how it worked for me. |
Seems odd that both mentions of Calendar Time in the documentation use the construction:
See: |
Hello!
I have the following snippet:
The problems , in my opinion, are :
If I'm missing something from the documentation, please let me know, but in my opinion this isn't the right behaviour for this function.
Thank you for your time!
The text was updated successfully, but these errors were encountered: