0

The following script calculates me next Friday and next Sunday date.

The problem : the use of .toISOString uses UTC time. I need to change with something that outputs local time. I'm very new to javascript so I can't find the right property to use instead of .toIsostring. What should I do ?

function nextWeekdayDate(date, day_in_week) {
  var ret = new Date(date || new Date());
  ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1);
  return ret;
}

let nextFriday = nextWeekdayDate(null, 5);
let followingSunday = nextWeekdayDate(nextFriday, 0);

console.log('Next Friday     : ' + nextFriday.toDateString() +
  '\nFollowing Sunday: ' + followingSunday.toDateString());

/* Previous code calculates next friday and next sunday dates */


var checkinf = nextWeekdayDate(null, 5);
var [yyyy, mm, dd] = nextFriday.toISOString().split('T')[0].split('-');
var checkouts = nextWeekdayDate(null, 7);
var [cyyy, cm, cd] = followingSunday.toISOString().split('T')[0].split('-');
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Francesco
  • 95
  • 6
  • If you haven't looked at the `Date.getTimezoneOffset` method, you might find it useful. It "returns the time zone difference, in MINUTES, from current locale (host system settings) to UTC." You can use this information to adjust your date object accordingly before converting it to a string (or before saving it to a database, etc.) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset – Cat Jun 08 '20 at 11:41
  • As with your [other question](https://stackoverflow.com/questions/62243185/date-script-doesnt-work-correctly-when-is-saturday), your issue is [how to format a date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1450.4192). In this case you can just use `nextFriday.toLocaleString('en-CA', {year:'numeric',month:'2-digit',day:'2-digit'})`. – RobG Jun 08 '20 at 12:03

2 Answers2

3

If you worry that the date is wrong in some timezones, try normalising the time

To NOT use toISO you can do this

const [dd1, mm1, yyyy1] = nextFriday.toLocaleString('en-GB', 
  { year: 'numeric', month: '2-digit', day: '2-digit' })
  .split("/")

function nextWeekdayDate(date, day_in_week) {
  var ret = new Date(date || new Date());
  ret.setHours(15, 0, 0, 0); // normalise
  ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1);
  return ret;
}

let nextFriday = nextWeekdayDate(null, 5);
let followingSunday = nextWeekdayDate(nextFriday, 0);

console.log('Next Friday     : ' + nextFriday.toDateString() +
  '\nFollowing Sunday: ' + followingSunday.toDateString());

/* Previous code calculates next friday and next sunday dates */


var checkinf = nextWeekdayDate(null, 5);
var [yyyy, mm, dd] = nextFriday.toISOString().split('T')[0].split('-');
var checkouts = nextWeekdayDate(null, 7);
var [cyyy, cm, cd] = followingSunday.toISOString().split('T')[0].split('-');

console.log(yyyy, mm, dd)

// not using UTC: 

const [dd1, mm1, yyyy1] = nextFriday.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).split("/")

console.log(yyyy1, mm1, dd1)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Just normalises the time to 15:00 which should handle all timezones correctly – mplungjan Jun 08 '20 at 10:32
  • Unless the user is in Pacific/Pago_Pago at -11:00 or Pacific/Tahiti at -10:00. Why not `date.toLocaleString('en-CA', {year:'numeric',month:'2-digit',day:'2-digit'})`? – RobG Jun 08 '20 at 11:53
  • @RobG I believe 15 - 11 is still today – mplungjan Jun 08 '20 at 12:21
  • 1
    Yes, but for -11 it's 15:00 plus 11, so 26:00 or 2:00 am tomorrow UTC. :-) You could do this using `nextFriday.setUTCMinutes(nextFriday.getUTCMinutes() - nextFriday.getTimezoneOffset())` then `nextFriday.toISOString().substring(0,10)`, but really this is a formatting issue so better dealt with that way. – RobG Jun 08 '20 at 12:26
  • Ok, I changed my answer. Thanks – mplungjan Jun 08 '20 at 12:28
  • @mplungjan what did you change? – Francesco Jun 08 '20 at 22:38
  • I added the alternative `const [dd1, mm1, yyyy1] = nextFriday.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).split("/")` – mplungjan Jun 09 '20 at 03:54
0

You are concerned that the [yyyy,mm,dd] is in UTC and not in current timzone?

The nextFriday is a date object. Would it work if you use the get-functions instead? e.g.

const nextFridayYear = nextFriday.getFullYear();
// get month is zero index based, i have added one
const nextFridayMonth = (nextFriday.getMonth() + 1).toString()
    .padStart(2, '0');
const nextFridayDay = today.getDate().toString()
    .padStart(2, '0');
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Rasmus Knap
  • 258
  • 2
  • 13
  • 1
    The possible problem is when you are near midnight in your locale, you can get the wrong date. I solved it by setting the time to 15:00 – mplungjan Jun 08 '20 at 10:33