Get week of year in JavaScript like in PHP

后端 未结 19 1345
南方客
南方客 2020-11-22 02:38

How do I get the current weeknumber of the year, like PHP\'s date(\'W\')?

It should be the ISO-8601 week number of year, weeks starting

相关标签:
19条回答
  • 2020-11-22 03:13

    The code snippet which works pretty well for me is this one:

    var yearStart = +new Date(d.getFullYear(), 0, 1);
    var today = +new Date(d.getFullYear(),d.getMonth(),d.getDate());
    var dayOfYear = ((today - yearStart + 1) / 86400000);
    return Math.ceil(dayOfYear / 7).toString();
    

    Note:
    d is my Date for which I want the current week number.
    The + converts the Dates into numbers (working with TypeScript).

    0 讨论(0)
提交回复
热议问题