Get week of year in JavaScript like in PHP

后端 未结 19 1665
南方客
南方客 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:01

    I found useful the Java SE's SimpleDateFormat class described on Oracle's specification: http://goo.gl/7MbCh5. In my case in Google Apps Script it worked like this:

    function getWeekNumber() {
      var weekNum = parseInt(Utilities.formatDate(new Date(), "GMT", "w"));
      Logger.log(weekNum);
    }
    

    For example in a spreadsheet macro you can retrieve the actual timezone of the file:

    function getWeekNumber() {
      var weekNum = parseInt(Utilities.formatDate(new Date(), SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "w"));
      Logger.log(weekNum);
    }
    

提交回复
热议问题