javascript - Moment.js 根据特定日期(也是过去几年)获取周数

标签 javascript momentjs week-number

我怎样才能从时刻 JS 获取过去日期的周数,只能从选定日期的时刻格式化对象中获取?

最佳答案

  $(document).ready(function(){
    var weeknumber = moment("12-25-1995", "MM-DD-YYYY").week();
    console.log(weeknumber);
  });

根据 momentjs 文档:

Because different locales define week of year numbering differently, Moment.js added moment#week to get/set the localized week of the year.

The week of the year varies depending on which day is the first day of the week (Sunday, Monday, etc), and which week is the first week of the year.

For example, in the United States, Sunday is the first day of the week. The week with January 1st in it is the first week of the year.

因此,如果您在获取正确的周数时遇到问题,请使用 .isoWeek()

$(document).ready(function(){
  var weeknumber = moment("11-26-2016", "MMDDYYYY").isoWeek();
  alert(weeknumber);
});

Example

关于javascript - Moment.js 根据特定日期(也是过去几年)获取周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953551/

相关文章:

javascript - 减少 CSS 中的上传按钮大小

javascript - 将对象数组一分为二 : are not all of them enumerated?

javascript - OnChange 事件计算延迟

momentjs - 使用 moment.js 格式化小时、分钟和秒

ruby - 使用 Ruby 计算一年中的周数

sql - Teradata 使用 SQL 代码获取周数

javascript - 如何设置 javascript 重定向以将用户发送到后台页面?看代码

javascript - 尝试实现 momentjs 以显示本地时间

javascript - 在 momentjs 中显示没有年份的长日期格式

sql-server - SQL Server - 给定周数,获取一周中的第一个日期?