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 分钟前
JavaWeb-day07-MySQL(DQL&多表设计)
java·开发语言·数据库·sql·mysql·javaweb·web
ShineWinsu3 小时前
对于Qt:QDial常见属性以及信号的解析
开发语言·c++·qt·gui·客户端·旋钮·qdial
Java后端的Ai之路5 小时前
RAG 还是 Agent 自主检索?Claude Code 删掉向量库之后,我把选型讲透了
开发语言·人工智能·prompt·agent·rag
starzy19905 小时前
深入理解Scala高阶函数:函数式编程核心
开发语言·scala
czq_268671948711 小时前
Python打卡第46天
开发语言·python
清晨Coding13 小时前
删了 Cookie 还能自动登录?Google系网站登录态自动恢复的原理分析
javascript
kyriewen13 小时前
我扫了 814 个依赖,97 个还敢要 Node 10
前端·javascript·程序员
Evand J14 小时前
【MATLAB例程介绍】(17)永磁同步电机(PMSM)基于自适应无迹卡尔曼滤波(AUKF)的电流噪声在线估计。测量噪声协方差自适应与转速估计对比仿真
开发语言·matlab·卡尔曼滤波·无迹卡尔曼滤波·pmsm·电机滤波
程序员大阳14 小时前
HTML+CSS+JS对话式实战(9)-项目总结与问题分析
javascript·css·html·实战·项目总结
BaiPeter15 小时前
ZPL文件预览
开发语言·c++