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

/**

* 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'));
}
相关推荐
凤山老林3 分钟前
04-Java JDK, JRE和JVM
java·开发语言·jvm
灵感__idea5 小时前
Hello 算法:贪心的世界
前端·javascript·算法
小成202303202656 小时前
Linux高级02
linux·开发语言
知行合一。。。6 小时前
Python--04--数据容器(总结)
开发语言·python
咸鱼2.07 小时前
【java入门到放弃】需要背诵
java·开发语言
ZK_H7 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐7 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子7 小时前
Java:异常(exception)
java·开发语言
lifewange7 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
cmpxr_7 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法