optimize date range display name

This commit is contained in:
MaysWind
2021-01-31 21:13:57 +08:00
parent 61267af634
commit 23186473cd
4 changed files with 52 additions and 5 deletions
+22
View File
@@ -222,6 +222,26 @@ function getDateRangeByDateType(dateType, firstDayOfWeek) {
}; };
} }
function isDateRangeMatchFullYears(minTime, maxTime) {
const minDateTime = parseDateFromUnixTime(minTime).set({ second: 0, millisecond: 0 });
const maxDateTime = parseDateFromUnixTime(maxTime).set({ second: 59, millisecond: 999 });
const firstDayOfYear = minDateTime.clone().startOf('year');
const lastDayOfYear = maxDateTime.clone().endOf('year');
return firstDayOfYear.unix() === minDateTime.unix() && lastDayOfYear.unix() === maxDateTime.unix();
}
function isDateRangeMatchFullMonths(minTime, maxTime) {
const minDateTime = parseDateFromUnixTime(minTime).set({ second: 0, millisecond: 0 });
const maxDateTime = parseDateFromUnixTime(maxTime).set({ second: 59, millisecond: 999 });
const firstDayOfMonth = minDateTime.clone().startOf('month');
const lastDayOfMonth = maxDateTime.clone().endOf('month');
return firstDayOfMonth.unix() === minDateTime.unix() && lastDayOfMonth.unix() === maxDateTime.unix();
}
function copyObjectTo(fromObject, toObject) { function copyObjectTo(fromObject, toObject) {
if (!isObject(fromObject)) { if (!isObject(fromObject)) {
return toObject; return toObject;
@@ -563,6 +583,8 @@ export default {
getThisYearLastUnixTime, getThisYearLastUnixTime,
getShiftedDateRange, getShiftedDateRange,
getDateRangeByDateType, getDateRangeByDateType,
isDateRangeMatchFullYears,
isDateRangeMatchFullMonths,
copyObjectTo, copyObjectTo,
copyArrayTo, copyArrayTo,
appendThousandsSeparator, appendThousandsSeparator,
+4 -2
View File
@@ -18,10 +18,12 @@ export default {
'long-without-second': 'M/D/YYYY HH:mm', 'long-without-second': 'M/D/YYYY HH:mm',
}, },
'year': { 'year': {
'long': 'YYYY' 'long': 'YYYY',
'short': 'YYYY'
}, },
'yearMonth': { 'yearMonth': {
'long': 'YYYY-M' 'long': 'YYYY-M',
'short': 'YYYY-M'
}, },
'monthDay': { 'monthDay': {
'long': 'M/D', 'long': 'M/D',
+4 -2
View File
@@ -18,10 +18,12 @@ export default {
'long-without-second': 'YYYY年M月D日 HH:mm', 'long-without-second': 'YYYY年M月D日 HH:mm',
}, },
'year': { 'year': {
'long': 'YYYY年' 'long': 'YYYY年',
'short': 'YYYY'
}, },
'yearMonth': { 'yearMonth': {
'long': 'YYYY年M月' 'long': 'YYYY年M月',
'short': 'YYYY-M'
}, },
'monthDay': { 'monthDay': {
'long': 'M月D日', 'long': 'M月D日',
+22 -1
View File
@@ -543,11 +543,32 @@ export default {
} }
} }
if (this.$utilities.isDateRangeMatchFullYears(query.startTime, query.endTime)) {
const displayStartTime = this.$utilities.formatUnixTime(query.startTime, this.$t('format.year.short'));
const displayEndTime = this.$utilities.formatUnixTime(query.endTime, this.$t('format.year.short'));
return displayStartTime !== displayEndTime ? `${displayStartTime} ~ ${displayEndTime}` : displayStartTime;
}
if (this.$utilities.isDateRangeMatchFullMonths(query.startTime, query.endTime)) {
const displayStartTime = this.$utilities.formatUnixTime(query.startTime, this.$t('format.yearMonth.short'));
const displayEndTime = this.$utilities.formatUnixTime(query.endTime, this.$t('format.yearMonth.short'));
return displayStartTime !== displayEndTime ? `${displayStartTime} ~ ${displayEndTime}` : displayStartTime;
}
const startTimeYear = this.$utilities.getYear(this.$utilities.parseDateFromUnixTime(query.startTime)); const startTimeYear = this.$utilities.getYear(this.$utilities.parseDateFromUnixTime(query.startTime));
const endTimeYear = this.$utilities.getYear(this.$utilities.parseDateFromUnixTime(query.endTime)); const endTimeYear = this.$utilities.getYear(this.$utilities.parseDateFromUnixTime(query.endTime));
const displayStartTime = this.$utilities.formatUnixTime(query.startTime, this.$t('format.date.short')); const displayStartTime = this.$utilities.formatUnixTime(query.startTime, this.$t('format.date.short'));
const displayEndTime = this.$utilities.formatUnixTime(query.endTime, this.$t(startTimeYear !== endTimeYear ? 'format.date.short' : 'format.monthDay.short')); const displayEndTime = this.$utilities.formatUnixTime(query.endTime, this.$t('format.date.short'));
if (displayStartTime === displayEndTime) {
return displayStartTime;
} else if (startTimeYear === endTimeYear) {
const displayShortEndTime = this.$utilities.formatUnixTime(query.endTime, this.$t('format.monthDay.short'));
return `${displayStartTime} ~ ${displayShortEndTime}`;
}
return `${displayStartTime} ~ ${displayEndTime}`; return `${displayStartTime} ~ ${displayEndTime}`;
} }