-2

I have try to get dates list between two date using JavaScript (I have already achieved in GMT).

Example:

fromDate - 2019-08-27

toDate - 2019-08-30

Date list

[2019-08-27, 2019-08-28, 2019-08-29, 2019-08-30]

I have already got this array using this following JavaScript

if(result.data.closurPeriods.length > 0) { 
    result.data.closurPeriods.forEach(closure => { 
        var start = closure.fromDate, //closure.fromDate = 2019-08-27
        end = new Date(closure.toDate), //closure.toDate = 2019-08-30
        currentDate = new Date(start);

        while (currentDate <= end) { 
            this.closurPeriods.push(this.datePipe.transform(new Date(currentDate), 'yyyy-MM-dd'));
            currentDate.setDate(currentDate.getDate() + 1);
        }
    });
}

The above JavaScript is working for only GTM and localtime(India). When I try to run this script in USA the date list array like this

[2019-08-28, 2019-08-28, 2019-08-29]

Because of UTC not accept this script.

My question is how to solve this above script in UTC

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ramesh S
  • 585
  • 3
  • 10
  • 32
  • There is nothing jQuery or Angular related to this question. It is pure JavaScript – mplungjan Jul 31 '19 at 07:31
  • @mplungjan : Yes your are right. Can you please tell me how to solve this `JavaScript` for `UTC` ? – Ramesh S Jul 31 '19 at 07:37
  • I was a little rash. `datePipe.transform` IS Angular – mplungjan Jul 31 '19 at 07:39
  • I would recommend using a DateTime library like Luxon/Moment to create DateTime objects in UTC. – Drew Reese Jul 31 '19 at 07:41
  • @DrewReese: Yes i have used `moment.twix` link http://jsfiddle.net/Lkzg1bxb/ . But i got error like twix not found – Ramesh S Jul 31 '19 at 07:43
  • This is kind of a duplicate of [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) but in an un–obvious way. ;-) – RobG Jul 31 '19 at 12:15

2 Answers2

1

2019-08-27 is parsed as UTC, but getDate and setDate are local. The USA is west of Greenwich, so new Date('2019-08-27') produces a local date for 2019-08-26, adding a day makes it 2019-08-27.

The same thing will happen for any timezone that has a negative offset.

A simple fix is to use all UTC, e.g.:

function fillRange(start, end) {
  let result = [start];
  let a = new Date(start);
  let b = new Date(end);
  while (a < b) {
    a.setUTCDate(a.getUTCDate() + 1);
    result.push(a.toISOString().substr(0,10));
  }
  return result;
}

let from = '2019-08-27';
let to = '2019-08-30';
console.log(fillRange(from, to));

However, I'd advise explicitly parsing the date and not to use the built–in parser. A simple parse function is 2 or 3 lines of code, or you can use one of many parsing and formatting libraries.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

Finally i got the solutions

var start = new Date(closure.fromDate); // 2019-07-27
var end = new Date(closure.toDate); // 2019-07-31
var currentDate = start;

while (currentDate <= end) {
   //this.closurPeriods.push(this.datePipe.transform(new Date(currentDate), 'yyyy-MM-dd'));
   var date = new Date(currentDate);
   var datewithouttimezone = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
   this.closurPeriods.push(this.datePipe.transform(new Date(datewithouttimezone), 'yyyy-MM-dd'));
   currentDate.setDate(currentDate.getDate() + 1);
}

Or

var start = new Date(closure.fromDate); // 2019-07-27
var end = new Date(closure.toDate); // 2019-07-31
var currentDate = start;

while (start < end) {
       start.setUTCDate(start.getUTCDate() + 1);
       this.closurPeriods.push(start.toISOString().substr(0, 10));
    }
Ramesh S
  • 585
  • 3
  • 10
  • 32