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'));
}
相关推荐
dlraba8021 天前
用 Python+OpenCV 实现实时文档扫描:从摄像头捕捉到透视矫正全流程
开发语言·python·opencv
芒果茶叶1 天前
并行SSR,SSR并行加载
前端·javascript·架构
一人の梅雨1 天前
1688 店铺商品全量采集与智能分析:从接口调用到供应链数据挖掘
开发语言·python·php
小何好运暴富开心幸福1 天前
C++之日期类的实现
开发语言·c++·git·bash
威风的虫1 天前
JavaScript中的axios
开发语言·javascript·ecmascript
老赵的博客1 天前
c++ 是静态编译语言
开发语言·c++
Terio_my1 天前
Python制作12306查票工具:从零构建铁路购票信息查询系统
开发语言·python·microsoft
bot5556661 天前
“企业微信iPad协议”静默 72 小时:一台被遗忘的测试机如何成为私域的逃生梯
javascript·面试
西洼工作室1 天前
浏览器事件循环与内存管理可视化
前端·javascript·css·css3
消失的旧时光-19431 天前
Kotlin when 用法完整分享
android·开发语言·kotlin