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'));
}
相关推荐
JIngJaneIL1 分钟前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
羸弱的穷酸书生12 分钟前
国网 i1协议 python实现
开发语言·python
电子硬件笔记14 分钟前
Python语言编程导论第三章 编写程序
开发语言·python·编辑器
布谷歌14 分钟前
在java中实现c#的int.TryParse方法
java·开发语言·python·c#
cooldream200919 分钟前
当代 C++ 的三大技术支柱:资源管理、泛型编程与模块化体系的成熟演进
开发语言·c++
洲星河ZXH35 分钟前
Java,集合框架体系
开发语言·windows
宠..40 分钟前
写一个感染型病毒
开发语言·安全·安全性测试
wheelmouse778841 分钟前
一个优雅、通用、零侵入的 CSV 导出工具类(Java 实战)
java·开发语言
阿蒙Amon1 小时前
JavaScript学习笔记:6.表达式和运算符
javascript·笔记·学习