本文主要是介绍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: weekOfYear 获取某天所处的一年中的周数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!