element-plus [i18n] [date-picker] 日期选择框格式化判断归属第几周不对

brjng4g3  于 2个月前  发布在  其他
关注(0)|答案(8)|浏览(41)

Bug Type: i18n

Environment

  • Vue Version: 3.2.45
  • Element Plus Version: 2.2.32
  • Browser / OS: UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
  • Build Tool: Vite

Reproduction

  • el-date-picker

Element Plus Playground

Steps to reproduce

设置了dayjs.en.weekStart = 1,一周也从周一开始了,才发现每年第一周完全不对

21 年 12-27 到22 年 1-2显示是 21 年第一周,紧接下一周 22 年1-3 到 1-9,变成22 年第二周,完完全全不对
既然设置一周开始是周一了,那一年第一周应该是从第一个周一开始啊

22 年 12-26 日到 23 年 1-1 号变成 22 年第一周了

What is Expected?

第一周展示准确

What is actually happening?

有误差

Additional comments

(empty)

yhuiod9q

yhuiod9q1#

@iamkun seems like a dayjs related issue 🤔

q7solyqu

q7solyqu2#

Day.js use yearStart config to determine which day is the first week of the year. You can change it via updateLocale method.

ref: https://day.js.org/docs/en/customization/customization

or you can load the locale file you need,
ref: https://day.js.org/docs/en/i18n/loading-into-nodejs

require('dayjs/locale/zh-cn')

dayjs.locale('zh-cn')
8i9zcol2

8i9zcol23#

我加了 dayjs.locale('en-au'),还是存在问题,我查了一年一周起始的标准。
假如有一个星期跨越了两年:如果这个星期有至少4天在上一年,那么这个星期就算上一年的最后一周;如果这个星期有至少4天在下一年,那么这个星期就算下一年的第一周!

目前组件一年一周起始是按 1-1 号来,它在的这周就是第一周

sd2nnvve

sd2nnvve4#

这是我写的 Java 端判断日期归属的代码

static {
format = new SimpleDateFormat("yyyy-MM-dd");
calendar = Calendar.getInstance();
//设置星期一为一周开始的第一天
calendar.setFirstDayOfWeek(Calendar.MONDAY);
//设置在一年中第一个星期所需最少天数
calendar.setMinimalDaysInFirstWeek(4);
}

public static String getYearAndWeek(String date) {
Date parse = null;
try {
parse = format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
calendar.setTime(parse);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
int year=calendar.get(Calendar.YEAR);
if (week == 1 && parse.getMonth() == 11) {
year = calendar.get(Calendar.YEAR) + 1;
}
return year+"-"+week;
}

public static void main(String[] args) {
    System.out.println(getYearAndWeek("2019-1-6"));
    System.out.println(getYearAndWeek("2019-1-7"));
}
4ioopgfo

4ioopgfo5#

Day.js follows ISO week date , that is, the first week of the year, hence, always contains 4 January.

ref: https://en.wikipedia.org/wiki/ISO_week_date

ref: https://day.js.org/docs/en/customization/customization

63lcw9qa

63lcw9qa6#

The weekOfYear depends on yearStart. You can set dayjs.en.yearStart = 4.
The 'dayjs/locale/en' and 'dayjs/locale/en-au' files don't have the param.
If yearStart don't set, the value of yearStart is 1, you can see this from dayjs.

7ajki6be

7ajki6be7#

i added dayjs.en.yearStart = 4, and tested again.the 2020 to 2023 are all coorrect now,but 2019 still has problems.
u see the last week between 2019 and 2020,still not right.

also the first week of 2013

wvyml7n5

wvyml7n58#

Because you use the format 'YYYY', here need the format 'gggg'.

https://day.js.org/docs/en/plugin/advanced-format

相关问题