Obtenha semana do ano em JavaScript, como em PHP


140

Como obtenho o número da semana atual do ano, como o PHP date('W')?

Deve ser o número de semanas ISO-8601 do ano, semanas a partir de segunda-feira.


1
Veja <a href=" javascript.about.com/library/blweekyear.htm "> <b> aqui </b> </a>, que foi o primeiro link fornecido quando pesquisei 'semana do ano em javascript'.
Pete Wilson

+1 Lol! Foi aí que obtive o trecho de mim mesmo, mas não conseguia me lembrar da fonte como o obtive há um tempo atrás.
Tom Chantler

@Pete: Esse código fica 22 como a semana atual. Embora deva ser 21
PeeHaa

@Pete:: D Nopez, um simples -1 não funciona: P Isso não receberia o número da semana ISO-8601. Uma semana na ISO-8601 começa na segunda-feira. A primeira semana é a semana com a primeira quinta-feira do ano. en.wikipedia.org/wiki/ISO-8601 . PS não fui eu quem votou contra você.
PeeHaa

Respostas:


276

Você deve conseguir o que deseja aqui: http://www.merlyn.demon.co.uk/js-date6.htm#YWD .

Um link melhor no mesmo site é: Trabalhando com semanas .

Editar

Aqui está um código com base nos links fornecidos e no eariler publicado por Dommer. Foi levemente testado em relação aos resultados em http://www.merlyn.demon.co.uk/js-date6.htm#YWD . Por favor, teste minuciosamente, nenhuma garantia fornecida.

Edit 2017

Houve um problema com as datas durante o período em que o horário de verão foi observado e os anos em que 1 de janeiro era sexta-feira. Corrigido usando todos os métodos UTC. A seguir, resultados idênticos ao Moment.js.

/* For a given date, get the ISO week number
 *
 * Based on information at:
 *
 *    http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
 *
 * Algorithm is to find nearest thursday, it's year
 * is the year of the week number. Then get weeks
 * between that date and the first day of that year.
 *
 * Note that dates in one year can be weeks of previous
 * or next year, overlap is up to 3 days.
 *
 * e.g. 2014/12/29 is Monday in week  1 of 2015
 *      2012/1/1   is Sunday in week 52 of 2011
 */
function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
    // Get first day of year
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    // Return array of year and week number
    return [d.getUTCFullYear(), weekNo];
}

var result = getWeekNumber(new Date());
document.write('It\'s currently week ' + result[1] + ' of ' + result[0]);

As horas são zeradas ao criar a data "UTC".

Versão protótipo minimizada (retorna apenas o número da semana):

Date.prototype.getWeekNumber = function(){
  var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  var dayNum = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};

document.write('The current ISO week number is ' + new Date().getWeekNumber());

Seção de teste

Nesta seção, você pode inserir qualquer data no formato AAAA-MM-DD e verificar se esse código fornece o mesmo número da semana que o número ISO da semana Moment.js ISO (testado em 50 anos entre 2000 e 2050).

Date.prototype.getWeekNumber = function(){
  var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  var dayNum = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};

function checkWeek() {
  var s = document.getElementById('dString').value;
  var m = moment(s, 'YYYY-MM-DD');
  document.getElementById('momentWeek').value = m.format('W');
  document.getElementById('answerWeek').value = m.toDate().getWeekNumber();      
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Enter date  YYYY-MM-DD: <input id="dString" value="2021-02-22">
<button onclick="checkWeek(this)">Check week number</button><br>
Moment: <input id="momentWeek" readonly><br>
Answer: <input id="answerWeek" readonly>


8
Esse código calcula 2 de janeiro de 2011 como a 53ª semana de 2010, onde deveria ser a 52ª. Isso funciona corretamente no código original, mas não na sua adaptação.
precisa

4
Você salvou minha bunda. Obrigado. Se você deseja contribuir com o código-fonte aberto, sugiro que você crie um patch para o método da interface do usuário do jQuery: $ .datepicker.iso8601Week (date), pois retorna apenas weekNo, mas não ano.
Christian

18
Hoje, 4 de janeiro de 2016, notei que era necessário adicionar d.setMilliseconds(0)também - continuava mostrando números de semanas diferentes para a mesma data, dependendo se eu usava nova Data () ou nova Data ("1/4/2016"). Apenas um alerta para outros que podem experimentar o mesmo.
Jacob Lauritzen

2
O código fornecido não segue a ISO 8601, é desativado por um
Eric Grange

2
Opa, você está certo, meu erro de digitação deveria ser '30-12-2015', o que funciona.
Ally


25

Como dito acima, mas sem uma classe:

let now = new Date();
let onejan = new Date(now.getFullYear(), 0, 1);
week = Math.ceil( (((now - onejan) / 86400000) + onejan.getDay() + 1) / 7 );

4
um-jan-tang! * ninja-roll *
CodeManX 01/09

2
Essa é a única resposta que busca o número correto da semana, mesmo na primeira semana do ano.
PrasadW

Nota a fazer (now.getTime() - onejan.getTime())para evitar problemas de construção.
Swoox

4
A pergunta feita pela ISO 8601 que isso ignora. Como uma resposta para a pergunta é simplesmente errado
havlock

23

Accordily http://javascript.about.com/library/blweekyear.htm

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    var millisecsInDay = 86400000;
    return Math.ceil((((this - onejan) /millisecsInDay) + onejan.getDay()+1)/7);
};

1
Conciso, mas trata o domingo como o primeiro dia da semana, de modo que o domingo 27 de dezembro de 2015 é o primeiro dia da semana 53 em vez do último dia da semana 52. Isso pode servir para alguns.
RobG 23/07

3
Eu acho que, uma vez que isso está sendo adicionado ao protótipo, é o que você esperaria, já que o Date trata o domingo como o primeiro dia.
Ed Sykes

Isso não terá problemas nos dias de horário de verão? Eu acho que não vai avançar até 01:00 durante o verão.
Hafthor

Além disso, isso tecnicamente não avança a semana até 0: 00: 00.001? Melhor usar o Math.floor?
Hafthor 17/02/16

11

A Date.format()biblioteca de Jacob Wright implementa a formatação de data no estilo da date()função do PHP e suporta o número da semana ISO-8601:

new Date().format('W');

Pode ser um pouco exagerado por apenas um número de semana, mas suporta a formatação no estilo PHP e é bastante útil se você estiver fazendo muito disso.


Boa solução para scripts hackeada-juntos rápidas :)
Steen Schütt

6
getWeekOfYear: function(date) {
        var target = new Date(date.valueOf()),
            dayNumber = (date.getUTCDay() + 6) % 7,
            firstThursday;

        target.setUTCDate(target.getUTCDate() - dayNumber + 3);
        firstThursday = target.valueOf();
        target.setUTCMonth(0, 1);

        if (target.getUTCDay() !== 4) {
            target.setUTCMonth(0, 1 + ((4 - target.getUTCDay()) + 7) % 7);
        }

        return Math.ceil((firstThursday - target) /  (7 * 24 * 3600 * 1000)) + 1;
    }

O código a seguir é independente do fuso horário (são utilizadas as datas UTC) e funciona de acordo com o https://en.wikipedia.org/wiki/ISO_8601


4

Achei útil a classe SimpleDateFormat do Java SE descrita na especificação da Oracle: http://goo.gl/7MbCh5 . No meu caso, no Script do Google Apps, funcionou assim:

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

Por exemplo, em uma macro de planilha, você pode recuperar o fuso horário real do arquivo:

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

4

Isso adiciona o método "getWeek" ao Date.prototype, que retorna o número da semana desde o início do ano. O argumento define qual dia da semana considerar o primeiro. Se nenhum argumento for aprovado, o primeiro dia será assumido no domingo.

/**
 * Get week number in the year.
 * @param  {Integer} [weekStart=0]  First day of the week. 0-based. 0 for Sunday, 6 for Saturday.
 * @return {Integer}                0-based number of week.
 */
Date.prototype.getWeek = function(weekStart) {
    var januaryFirst = new Date(this.getFullYear(), 0, 1);
    if(weekStart !== undefined && (typeof weekStart !== 'number' || weekStart % 1 !== 0 || weekStart < 0 || weekStart > 6)) {
      throw new Error('Wrong argument. Must be an integer between 0 and 6.');
    }
    weekStart = weekStart || 0;
    return Math.floor((((this - januaryFirst) / 86400000) + januaryFirst.getDay() - weekStart) / 7);
};

1
A primeira semana do calendário de 2016 começa no dia 4 de janeiro na Alemanha , mas sua função começa a contar novamente a partir de 0 a partir de 1º de janeiro. Ele também retorna números incorretos no final do ano, por exemplo, 52 para 31/11/2018 (53ª semana), embora já seja a 1ª semana do calendário de 2019 : new Date(Date.UTC(2018,11, 31)).getWeek(1)+1(segunda-feira é o 1º dia da semana na Alemanha).
CodeManX

Foi assim que foi planejado e, acredito, é o caso de uso mais provável. Caso contrário, os três primeiros dias de 2016 cairiam. Considera-se que os primeiros dias do mês compreendem a primeira semana desse mês, independentemente de quais e quantos dias existem. Se você precisar que a função funcione de maneira diferente, poderá ajustá-la de acordo com suas necessidades. Da mesma forma, se uma semana se enquadra no ano especificado e no ano seguinte, ela pode ser chamada de última semana daquele ano e também da primeira semana do ano seguinte (de acordo com a lógica atual).
Tigran

Obrigado pela informação. Acabei usando a solução da RobG, que implementa as datas da semana ISO8601 corretamente (os últimos dias de dezembro e os primeiros dias de janeiro podem pertencer às semanas 52, 53 ou 1: en.m.wikipedia.org/wiki/ISO_week_date
CodeManX

4

Obter o número da semana de qualquer data

function week(year,month,day) {
    function serial(days) { return 86400000*days; }
    function dateserial(year,month,day) { return (new Date(year,month-1,day).valueOf()); }
    function weekday(date) { return (new Date(date)).getDay()+1; }
    function yearserial(date) { return (new Date(date)).getFullYear(); }
    var date = year instanceof Date ? year.valueOf() : typeof year === "string" ? new Date(year).valueOf() : dateserial(year,month,day), 
        date2 = dateserial(yearserial(date - serial(weekday(date-serial(1))) + serial(4)),1,3);
    return ~~((date - date2 + serial(weekday(date2) + 5))/ serial(7));
}

Exemplo

console.log(
    week(2016, 06, 11),//23
    week(2015, 9, 26),//39
    week(2016, 1, 1),//53
    week(2016, 1, 4),//1
    week(new Date(2016, 0, 4)),//1
    week("11 january 2016")//2
);

1
Não acredito, mas essa função é a única que funcionou o tempo todo! A resposta aceita apareceu quando passou o horário de verão, outros disseram '0' como o número da semana em determinados anos. - e alguns se baseavam nas funções UTC que, às vezes, retornavam no dia anterior, atribuindo-o à semana '53' ou '54'. Infelizmente eu preciso da semana para começar em um domingo e este código é muito difícil de entender ...
Melissa Zachariadis

@MelissaZachariadis disse I need the week to begin on a Sunday; Eu acho que a mudança só necessário é em função da semana () 's .getDay()+1de ser alterado para.getDay()
Rafa

4

O código abaixo calcula o número da semana ISO 8601 correto. Ele corresponde ao PHP date("W")para todas as semanas entre 1/1/1970 e 1/1/2100.

/**
 * Get the ISO week date week number
 */
Date.prototype.getWeek = function () {
  // Create a copy of this date object
  var target = new Date(this.valueOf());

  // ISO week date weeks start on Monday, so correct the day number
  var dayNr = (this.getDay() + 6) % 7;

  // ISO 8601 states that week 1 is the week with the first Thursday of that year
  // Set the target date to the Thursday in the target week
  target.setDate(target.getDate() - dayNr + 3);

  // Store the millisecond value of the target date
  var firstThursday = target.valueOf();

  // Set the target to the first Thursday of the year
  // First, set the target to January 1st
  target.setMonth(0, 1);

  // Not a Thursday? Correct the date to the next Thursday
  if (target.getDay() !== 4) {
    target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
  }

  // The week number is the number of weeks between the first Thursday of the year
  // and the Thursday in the target week (604800000 = 7 * 24 * 3600 * 1000)
  return 1 + Math.ceil((firstThursday - target) / 604800000);
}

Fonte: Taco van den Broek


Se você não gosta de estender protótipos, aqui está uma função:

function getWeek(date) {
  if (!(date instanceof Date)) date = new Date();

  // ISO week date weeks start on Monday, so correct the day number
  var nDay = (date.getDay() + 6) % 7;

  // ISO 8601 states that week 1 is the week with the first Thursday of that year
  // Set the target date to the Thursday in the target week
  date.setDate(date.getDate() - nDay + 3);

  // Store the millisecond value of the target date
  var n1stThursday = date.valueOf();

  // Set the target to the first Thursday of the year
  // First, set the target to January 1st
  date.setMonth(0, 1);

  // Not a Thursday? Correct the date to the next Thursday
  if (date.getDay() !== 4) {
    date.setMonth(0, 1 + ((4 - date.getDay()) + 7) % 7);
  }

  // The week number is the number of weeks between the first Thursday of the year
  // and the Thursday in the target week (604800000 = 7 * 24 * 3600 * 1000)
  return 1 + Math.ceil((n1stThursday - date) / 604800000);
}

Uso da amostra:

getWeek(); // Returns 37 (or whatever the current week is)
getWeek(new Date('Jan 2, 2011')); // Returns 52
getWeek(new Date('Jan 1, 2016')); // Returns 53
getWeek(new Date('Jan 4, 2016')); // Returns 1

Eu gosto dessa função, mas tenho uma pergunta; O que devo fazer se quiser voltar ao domingo? Não tenho ideia do que a +6 ) % 7parte faz. Obrigado por uma esfoliação!
NoobishPro 22/10/16

1
A semana ISO do @Babydead começa na segunda-feira, mas o JavaScript getDay()começa no domingo, portanto, se você quiser que comece no domingo, poderá remover a correção:var nDay = date.getDay();
thdoan

Eu tentei mais de 8 implementações JS diferentes para obter o número da semana. Essa é a única função que funciona, mas somente se eu alterar todos os getters e setters para getUTC .. e setUTC .. Não sei por quê. Eu estava testando com isso: 2017-07-17T00: 00: 00.000Z (semana 29) 2017-07-23T23: 59: 59.000Z (semana 29) 2021-01-04T00: 00: 00.000Z (semana 1)
psicótico brm


2

O trecho de código que funciona muito bem para mim é este:

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();

Nota:
dé a minha data para a qual desejo o número da semana atual.
O +converte as datas em números (trabalhando com TypeScript).


1

Aqui está minha implementação para calcular o número da semana em JavaScript. corrigido também para as compensações no horário de verão e inverno. Eu usei a definição da semana deste artigo: ISO 8601

As semanas são de segunda a domingo e o dia 4 de janeiro é sempre a primeira semana do ano.

// add get week prototype functions
// weeks always start from monday to sunday
// january 4th is always in the first week of the year
Date.prototype.getWeek = function () {
    year = this.getFullYear();
    var currentDotw = this.getWeekDay();
    if (this.getMonth() == 11 && this.getDate() - currentDotw > 28) {
        // if true, the week is part of next year 
        return this.getWeekForYear(year + 1);
    }
    if (this.getMonth() == 0 && this.getDate() + 6 - currentDotw < 4) {
        // if true, the week is part of previous year
        return this.getWeekForYear(year - 1);
    }
    return this.getWeekForYear(year);
}

// returns a zero based day, where monday = 0
// all weeks start with monday
Date.prototype.getWeekDay = function () {
    return  (this.getDay() + 6) % 7;
}

// corrected for summer/winter time
Date.prototype.getWeekForYear = function (year) {
    var currentDotw = this.getWeekDay();
    var fourjan = new Date(year, 0, 4);
    var firstDotw = fourjan.getWeekDay();
    var dayTotal = this.getDaysDifferenceCorrected(fourjan) // the difference in days between the two dates.
    // correct for the days of the week
    dayTotal += firstDotw; // the difference between the current date and the first monday of the first week, 
    dayTotal -= currentDotw; // the difference between the first monday and the current week's monday
    // day total should be a multiple of 7 now
    var weeknumber = dayTotal / 7 + 1; // add one since it gives a zero based week number.
    return weeknumber;
}

// corrected for timezones and offset
Date.prototype.getDaysDifferenceCorrected = function (other) {
    var millisecondsDifference = (this - other);
    // correct for offset difference. offsets are in minutes, the difference is in milliseconds
    millisecondsDifference += (other.getTimezoneOffset()- this.getTimezoneOffset()) * 60000;
    // return day total. 1 day is 86400000 milliseconds, floor the value to return only full days
    return Math.floor(millisecondsDifference / 86400000);
}

para teste, usei os seguintes testes JavaScript no Qunit

var runweekcompare = function(result, expected) {
    equal(result, expected,'Week nr expected value: ' + expected + ' Actual value: ' + result);
}

test('first week number test', function () {
    expect(5);
    var temp = new Date(2016, 0, 4); // is the monday of the first week of the year
    runweekcompare(temp.getWeek(), 1);
    var temp = new Date(2016, 0, 4, 23, 50); // is the monday of the first week of the year
    runweekcompare(temp.getWeek(), 1);
    var temp = new Date(2016, 0, 10, 23, 50); // is the sunday of the first week of the year
    runweekcompare(temp.getWeek(), 1);
    var temp = new Date(2016, 0, 11, 23, 50); // is the second week of the year
    runweekcompare(temp.getWeek(), 2);
    var temp = new Date(2016, 1, 29, 23, 50); // is the 9th week of the year
    runweekcompare(temp.getWeek(), 9);
});

test('first day is part of last years last week', function () {
    expect(2);
    var temp = new Date(2016, 0, 1, 23, 50); // is the first last week of the previous year
    runweekcompare(temp.getWeek(), 53);
    var temp = new Date(2011, 0, 2, 23, 50); // is the first last week of the previous year
    runweekcompare(temp.getWeek(), 52);
});

test('last  day is part of next years first week', function () {
    var temp = new Date(2013, 11, 30); // is part of the first week of 2014
    runweekcompare(temp.getWeek(), 1);
});

test('summer winter time change', function () {
    expect(2);
    var temp = new Date(2000, 2, 26); 
    runweekcompare(temp.getWeek(), 12);
    var temp = new Date(2000, 2, 27); 
    runweekcompare(temp.getWeek(), 13);
});

test('full 20 year test', function () {
    //expect(20 * 12 * 28 * 2);
    for (i = 2000; i < 2020; i++) {
        for (month = 0; month < 12; month++) {
            for (day = 1; day < 29 ; day++) {
                var temp = new Date(i, month, day);
                var expectedweek = temp.getWeek();
                var temp2 = new Date(i, month, day, 23, 50);
                var resultweek = temp.getWeek();
                equal(expectedweek, Math.round(expectedweek), 'week number whole number expected ' + Math.round(expectedweek) + ' resulted week nr ' + expectedweek);
                equal(resultweek, expectedweek, 'Week nr expected value: ' + expectedweek + ' Actual value: ' + resultweek + ' for year ' + i + ' month ' + month + ' day ' + day);
            }
        }
    }
});

0

O número desta semana tem sido uma verdadeira dor no a **. A maioria dos scripts da rede não funcionou para mim. Eles trabalharam a maior parte do tempo, mas todos quebraram em algum momento, especialmente quando o ano mudou e a última semana do ano foi repentinamente a primeira semana do próximo ano etc. semana 53).

Nota: Os exemplos foram criados para funcionar com semanas europeias (segunda-feira primeiro)!

getWeek ()

Date.prototype.getWeek = function(){

    // current week's Thursday
    var curWeek = new Date(this.getTime());
        curWeek.setDay(4);

    // Get year's first week's Thursday
    var firstWeek = new Date(curWeek.getFullYear(), 0, 4);
        firstWeek.setDay(4);

    return (curWeek.getDayIndex() - firstWeek.getDayIndex()) / 7 + 1;
};

setDay ()

/**
* Make a setDay() prototype for Date
* Sets week day for the date
*/
Date.prototype.setDay = function(day){

    // Get day and make Sunday to 7
    var weekDay = this.getDay() || 7;
    var distance = day - weekDay;
    this.setDate(this.getDate() + distance);

    return this;
}

getDayIndex ()

/*
* Returns index of given date (from Jan 1st)
*/

Date.prototype.getDayIndex = function(){
    var start = new Date(this.getFullYear(), 0, 0);
    var diff = this - start;
    var oneDay = 86400000;

    return Math.floor(diff / oneDay);
};

Eu testei isso e parece estar funcionando muito bem, mas se você notar uma falha, informe-me.


0

Tentei muito obter o código mais curto para obter o número da semana conforme a ISO.

Date.prototype.getWeek=function(){
    var date=new Date(this);
    date.setHours(0,0,0,0);
    return Math.round(((date.setDate(this.getDate()+2-(this.getDay()||7))-date.setMonth(0,4))/8.64e7+3+(date.getDay()||7))/7)+"/"+date.getFullYear();}

A variável dateé necessária para evitar alterar o original this. Usei os valores de retorno de setDate()e setMonth()dispenso getTime()para economizar o comprimento do código e usei um número expontial por milissegundos de dia em vez de uma multiplicação de elementos únicos ou um número com cinco zeros. thisé Data ou Número de milissegundos, o valor de retorno é, Stringpor exemplo, "49/2017".



0

Outra opção baseada em biblioteca: use d3-time-format:

const formatter = d3.timeFormat('%U');
const weekNum = formatter(new Date());

0

Solução mais curta para Angular2 + DatePipe, ajustado para ISO-8601:

import {DatePipe} from "@angular/common";

public rightWeekNum: number = 0;
  
constructor(private datePipe: DatePipe) { }
    
calcWeekOfTheYear(dateInput: Date) {
  let falseWeekNum = parseInt(this.datePipe.transform(dateInput, 'ww'));
  this.rightWeekNum = falseWeekNum ? falseWeekNum : falseWeekNum-1;
}

-1
now = new Date();
today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
firstOfYear = new Date(now.getFullYear(), 0, 1);
numOfWeek = Math.ceil((((today - firstOfYear) / 86400000)-1)/7);
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.