Jin's rambling

moment.js and week of year

moment.js를 사용해서 weekly data를 다룰 때 유용한 snippet
const moment = require('moment');

let today = moment();
// output: moment("2019-03-11T00:00:00.000")

// getting current ISO week
today.format('WW')
// output: '11'

// getting ISO week of a given date
moment('2019-07-01', 'YYYY-MM-DD').format('WW')
// output: '27'

// getting start of current week
moment(today).startOf('isoWeek')
// output: moment("2019-03-11T00:00:00.000")

// getting start of current week, formatted
moment(today).startOf('isoWeek').format('YYYY-MM-DD')
// output: '2019-03-11'

// getting start of a given ISO week (in this case 5th week).
moment('5', 'WW').startOf('isoWeek').format('YYYY-MM-DD')
// output: '2019-01-28'

// getting start of a given ISO week from a given year (in this case 5th week of 2017).
moment('2017-05', 'YYYY-WW').startOf('isoWeek').format('YYYY-MM-DD')
// output: '2017-01-30'

// getting end of current week
moment(today).endOf('isoWeek')
// output: moment("2019-03-17T23:59:59.999")

// getting end of current week, formatted
moment(today).endOf('isoWeek').format('YYYY-MM-DD')
// output: '2019-03-17'

// getting end of a given ISO week (in this case 5th week).
moment('5', 'WW').endOf('isoWeek').format('YYYY-MM-DD')
// output: '2019-02-03'

// getting end of a given ISO week from a given year (in this case 5th week of 2017).
moment('2017-05', 'YYYY-WW').endOf('isoWeek').format('YYYY-MM-DD')
// output: '2017-02-05'