Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
Javascript get localized weekday without a DateTime object

I at the moment have the day values as strings. (e.g. "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday") but no date.

The days describe opening hours of a shop and I would like to translate these string values dynamically based of the locale set on the shop.

I noticed that JS have the wonderful method Date.prototype.toLocaleDateString() but it doesn't seem like that I can get the localized string without providing a date. How would you go about this?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Have a look at Date.prototype.getDay

const weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

const date = new Date();

// get first day of week, and then add the weekday

let day = date.getDate() - date.getDay() + weekdays.indexOf("Saturday");

date.setDate(day)

console.log(date.toString());

about 4 years ago · Santiago Trujillo Relatório

0

You can use the Date() perhaps

// current date

let date = new Date();

Additionally, we can get a day of week:

getDay()

Get the day of week, from 0 (Sunday) to 6 (Saturday).

When using the Date() method you can also check for year, month, day and time. Maybe if you want to check so that it´s not on christmas day or after closing hours etc. Should work fine!

Methods:

getFullYear()

Get the year (4 digits)

getMonth()

Get the month, from 0 to 11.

getDate()

Get the day of month, from 1 to 31, the name of the method does look a little bit strange.

getHours(), getMinutes(), getSeconds(), getMilliseconds()

Get the corresponding time components.

about 4 years ago · Santiago Trujillo Relatório

0

Just create a new date object pointing to any Monday, then add days consecutively while calling date.ToLocaleDateString(LOC, {weekday: 'long'}), replace LOC with any locale you wish to use, to define in which language you'd like to get the week names.

function listDayNames(elem, locale)
{
  let e = document.getElementById(elem);
  let date = new Date('2022/08/22');
  for(let i=0;i<=6;i++) {
    e.innerHTML += date.toLocaleDateString(locale, { weekday: 'long' }) + '<br>';        
    date.setDate(date.getDate() + 1);
  }
  e.innerHTML += '<hr>';
}

let date = new Date();
listDayNames('days', 'en-US');
listDayNames('days', 'de-DE');
<p id="days"></p>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda