Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70433

Puzzling Issue while working with Date() in Ionic (QUICK HELP WILL BE HIGHLY APPRECIATED)

$
0
0

@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 stores pickup objects differing in date and time.
Each successive pickup 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-built Date().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 in repeated_pickups array, I am getting last date created in all objects.

Further, during debugging in which I commented out the entire for loop and added pickup.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 placing pickup.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 to pickup.date that I get last incremented date in all objects inside repeated_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

Read full topic


Viewing all articles
Browse latest Browse all 70433

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>