javascript: weekOfYear 获取某天所处的一年中的周数

 /**
 * Get the number of week for a specific day in a year. It will return 1 to 53.
 */

// 方法 1
export function weekOfYear(year: number, month: number, day: number) {
  const startDate = new Date(year, 0, 1);
  const currentDate = new Date(year, month - 1, day);
  var days = Math.floor(
    (currentDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000)
  );

  return Math.ceil(days / 7);
}


// 方法 2
import * as moment from 'moment';
export function weekOfMonth(year: number, month: number, day: number) {
  const date = new Date(year, month - 1, day);
  return parseInt(moment(date).format('W'));
}

你可能感兴趣的:(Javascript,/,Typescript,javascript,开发语言,ecmascript)