0

I have an array of days eg :

days_of_week: ["SUNDAY", "TUESDAY", "THURSDAY"]
hours : 19 
minute : 30

this array denotes the days in UTC timezone.

I want to display the array of days in local timezone.

So, the above example would look like (at IST (UTC+5:30))

days : Mon, Wed, Fri
hrs : 01
min : 00

I can convert the time but I'm not able to figure out the offset calculation for days.

1 Answers1

1

You should be able to create a relatively simple function that will calculate the desired offsets, both in time and days.

NB: This will only work for fixed timezone offsets. IST will work because there's currently no daylight savings time. This wouldn't work for, say Pacific Time or Western European Time.

let all_days_of_week = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"];

days_of_week = ["SUNDAY", "TUESDAY", "THURSDAY"]
hours = 19 
minute = 30;

function getOffsetTimes(hour, minutes, fixedOffsetMinutes, daysOfWeek) {

    let time = hour*60 + minutes;
    let offsetTime = time + fixedOffsetMinutes;
    let offsetDays = 0;
    if (offsetTime >= 1440) {
        offsetDays++;
        offsetTime = offsetTime % 1440;
    }
    
    let newDaysOfWeek = daysOfWeek.map((d, i) => (all_days_of_week.findIndex(dow => dow === d) + offsetDays) % 7 ).map(n => all_days_of_week[n]);
    
    return { hour: Math.floor(offsetTime/60), minute: (offsetTime % 60), daysOfWeek:  newDaysOfWeek}
}

console.log(getOffsetTimes(19, 30, 330, ["SUNDAY", "TUESDAY", "THURSDAY"]));
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40