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'));
}
相关推荐
叶小鸡15 分钟前
Java 篇-项目实战-AI 天机学堂(从 0 到 1)-day1
java·开发语言
大家的林语冰1 小时前
CSS 已死?DOM 性能黑洞!Pretext 排版革命让你在文本间跳舞,没有 DOM 也能纵享丝滑~
前端·javascript·css
楼田莉子2 小时前
C++17新特性:__had_include/属性/求值顺序规则
开发语言·c++·后端
kyriewen2 小时前
用魔法打败魔法:我让AI替我去面试前端岗,AI面试官给我打了92分,还发了offer
前端·javascript·面试
香蕉鼠片2 小时前
Python进阶学习
开发语言·python
摇滚侠2 小时前
Java 零基础全套教程,File 类与 IO 流,笔记 177-178
java·开发语言·笔记
ytttr8733 小时前
OPC UA 协议栈 C 语言实现
c语言·开发语言·mfc
song5013 小时前
Ascend C 算子开发:从入门到上手
c语言·开发语言·图像处理·人工智能·分布式·flutter·交互
小a杰.3 小时前
Ascend C编程语言进阶:高性能算子开发技巧
android·c语言·开发语言
全糖可乐气泡水3 小时前
Codex适配国产信创环境安装部署与技术适配全解析
开发语言·git·python·算法·百度