How to use `moment` to convert timestamp to week of the year

51 Views Asked by At

I need to convert the timestamp to the week of the year, and use it with the time selector of antd. I only found the following method, which is the method to get the week of the month. Does anyone know how to get the week of the year?

function weekOfMonth (m: any) {
    return Math.ceil(m.date() / 7);
}

function getYearWeek (weekData: any) {
    const startDate = moment(weekData).startOf('week');
    let weekMonth = weekOfMonth(startDate);
    let date = startDate.format('YYYY年MM月');
    return `${date}第${weekMonth}周`;
}
1

There are 1 best solutions below

0
Kan Robert On
  1. week of the year

w 1 2 ... 52 53

wo 1st 2nd ... 52nd 53rd

ww 01 02 ... 52 53

  1. week of the year (ISO)

W 1 2 ... 52 53

Wo 1st 2nd ... 52nd 53rd

WW 01 02 ... 52 53

Just moment().format('W') will do