Skip to content

Commit

Permalink
Merge pull request #3498 from meeting-room-booking-system/Issue_3496
Browse files Browse the repository at this point in the history
Issue 3496
  • Loading branch information
campbell-m authored Sep 11, 2023
2 parents cd7c53e + 349ee9c commit 3f88f78
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions web/js/datepicker.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,25 @@ function iPadMobileFix() {
?>
function getISODate(year, month, day)
{
<?php // toISOString() converts to UTC, so make the date a UTC date ?>
var date = new Date(Date.UTC(year, month, day));
return date.toISOString().split('T')[0];
}


// Given a JavaScript Date object returns a date string in YYYY-MM-DD
// format. (Note that toISOString() returns a date in UTC time).
function getLocalISODateString(date)
{
var month = (date.getMonth() + 1).toString().padStart(2, '0');
var day = date.getDate().toString().padStart(2, '0');
var year = date.getFullYear().toString();

return [year, month, day].join('-');
}



<?php
// Functions to find the start and end dates of a week and month given a
// date in YYYY-MM-DD format.
Expand All @@ -51,30 +66,39 @@ function getISODate(year, month, day)
?>

function weekStart(date, weekStarts) {
var d = new Date(date);
<?php
// Need to add a time to make sure the date is interpreted as local rather than UTC.
// "When the time zone offset is absent, date-only forms are interpreted as a UTC time and
// date-time forms are interpreted as local time. This is due to a historical spec error
// that was not consistent with ISO 8601 but could not be changed due to web compatibility."
?>
var d = new Date(date + "T00:00:00");
var diff = d.getDay() - weekStarts;
if (diff < 0)
{
diff += 7;
}
d.setDate(d.getDate() - diff);
return d.toISOString().split('T')[0];
return getLocalISODateString(d);
}

function weekEnd(date, weekStarts) {
var d = new Date(weekStart(date, weekStarts));
<?php // Need to add a time to make sure the date is interpreted as local rather than UTC. ?>
var d = new Date(weekStart(date, weekStarts) + "T00:00:00");
d.setDate(d.getDate() + 6);
return d.toISOString().split('T')[0];
return getLocalISODateString(d);
}

function monthStart(date) {
var d = new Date(date);
<?php // Need to add a time to make sure the date is interpreted as local rather than UTC. ?>
var d = new Date(date + "T00:00:00");
d.setDate(1);
return d.toISOString().split('T')[0];
return getLocalISODateString(d);
}

function monthEnd(date) {
var d = new Date(date);
<?php // Need to add a time to make sure the date is interpreted as local rather than UTC. ?>
var d = new Date(date + "T00:00:00");
<?php
// Set the date to the first of the month, because otherwise we will
// advance by two months when incrementing the month below if we're at the
Expand All @@ -83,25 +107,26 @@ function monthEnd(date) {
d.setDate(1);
d.setMonth(d.getMonth() + 1);
d.setDate(0);
return d.toISOString().split('T')[0];
return getLocalISODateString(d);
}


// Returns an array of dates in the range startDate..endDate, optionally
// excluding hidden days.
function datesInRange(startDate, endDate, excludeHiddenDays) {
var result=[];
var e=new Date(endDate);
<?php // Need to add a time to make sure the date is interpreted as local rather than UTC. ?>
var e=new Date(endDate + "T00:00:00");
var hiddenDays = [<?php echo implode(',', $hidden_days)?>];

<?php // dates can be compared using > and < but not == or === ?>
for (var d=new Date(startDate); !(d>e); d.setDate(d.getDate()+1))
for (var d=new Date(startDate + "T00:00:00"); !(d>e); d.setDate(d.getDate()+1))
{
if (excludeHiddenDays && (hiddenDays.indexOf(d.getDay()) >= 0))
{
continue;
}
result.push(d.toISOString().split('T')[0]);
result.push(getLocalISODateString(d));
}

return result;
Expand Down

0 comments on commit 3f88f78

Please sign in to comment.