Zego Tech
Zego Tech

Reputation: 21

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

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}周`;
}

Upvotes: 1

Views: 58

Answers (1)

Kan Robert
Kan Robert

Reputation: 299

  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

Upvotes: 0

Related Questions