@waleedshkt wrote:
I am struggling to get my head around the puzzling issue arising when setting and manipulating date and time.
Here is what I am doing:
I am creating an array
repeated_pickups
that storespickup
objects differing in date and time.
Each successivepickup
object in array is containing a new date as a string but the same time, a string too. The new date is different from previous as it is set through number of days incremental using JS in-builtDate().set()
method.Here is my code:
let pickup = this.pickup.getPickupObject(); // pickup object { date: '', time: ''} imported from provider const time = this.showTime; // string containg current time in ''09:30 AM" format let repeated_pickups = []; //creating pickup object with same time and date incremented by 1 day for next five consecutive days for(var i = 0; i < 5; i++) { pickup.time = time; //setting time to pickup object let date: any = new Date(); // today's date date.setDate(date.getDate() + ( i + 1)); // adding (i + 1) days to today date let next_date = new Date(date).toISOString().split('T')[0]; // formatting the new date in form 'YYYY-mm-dd' pickup.date = next_date; // setting a date string in pickup object with new_date repeated_pickups.push(pickup); }
Now issue here is that instead of getting increasing dates in each
pickup
object inrepeated_pickups
array, I am getting last date created in all objects.Further, during debugging in which I commented out the entire
for
loop and addedpickup.time = time;
after the following code:let pickup = this.pickup.getPickupObject(); // pickup object { date: '', time: ''} imported from provider const time = this.showTime; // string containg current time in ''09:30 AM" format let repeated_pickups = [];
I got pickup object containing some out-of-blue date string with time too, despite not setting it.
Even with
for
loop code added in debugging but this time placingpickup.time = time;
back in its original position inside the loop, I got pickup object which should be empty before loop containing date and time values set inside loop.I also noted that through
console.log()
that next_date increments to new date successfully. But it is just when assigning it topickup.date
that I get last incremented date in all objects insiderepeated_pickups
array.Can someone please have a look at this and let me know where I am getting wrong.
I’ll greatly appreciate the help!
Posts: 1
Participants: 1