/**
* Get the number of week for a specific day in a year. It will return 1 to 53.
*/
TypeScript
// 方法 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'));
}