gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:11:03 27 4
gpt4 key购买 nike

我怎样才能从时刻 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com