3

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?

4 Answers4

1

Have a look at Date.prototype.getDay

// First day of the week depends on the order of this array
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(prompt("Day?", "Monday"));

date.setDate(day)

console.log(date.toString());
Lakshaya U.
  • 1,089
  • 1
  • 5
  • 21
  • 1
    This doesn't answer the question, which was how to convert an English week day string to a translated one based on the locale. – CherryDT Aug 24 '22 at 09:45
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.

Wanz
  • 137
  • 9
  • 2
    This doesn't answer the question, which was how to convert an English week day string to a translated one based on the locale. – CherryDT Aug 24 '22 at 09:57
  • 1
    This was my first approach, but the date is not of the essence here. In fact I am not the one handling whether the shop is closed or not, I get that information from the backend team :) so my only real task here is to get "monday" translated into "montag" if it's a German shop we're talking about. I don't know if it's at all possible without a date object to work with. –  Aug 24 '22 at 09:58
0

You can create a dummy date based on the weekday, choosing the year and month such that a day value of 0 matches a Sunday (yes, you can specify the 0th day too).

Then you can use toLocaleDateString with the date to ask for the translated weekday string.

(I also invoked an Intl.DateTimeFormatter here to get the user's default locale, because we cannot specify an options object without also specifying a locale.)

const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']

function translateWeekday (weekdayString) {
  const weekdayIndex = WEEKDAYS.indexOf(weekdayString.toLowerCase())
  if (weekdayIndex < 0) throw new Error(`Unknown weekday "${weekdayString}"`)

  const dummyDate = new Date(2001, 0, weekdayIndex)
  const locale = new Intl.DateTimeFormat().resolvedOptions().locale

  return dummyDate.toLocaleDateString(locale, { weekday: 'long' })
}

On my machine with German locale, translateWeekday('Wednesday') returns 'Mittwoch' for example.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
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>
SaschaM78
  • 4,376
  • 4
  • 33
  • 42