0

I am adding some epoch time to my date variable and then converting it to iso to put it inside db. but my code is not geting correct time. my code is not stoping to execute => var releaseDate = new Date(epochDate).toISOString(); how should I make it happen

my code output is

epoch and now =
Mon Dec 21 2020 02:16:56 GMT+0530 (IST)604800000
1618003233700
2020-12-20T20:46:56.000Z

date should be today + 7 days but it didn't change.

async function schedule(data, epochDate) {
    const endpoint = config.main+"/schedule"
    const opt = {
        headers: {
            "id": 0
        }
    }
 
    //add 7 days in epoch time milliseconds  
    epochDate += 604800000; 
    const now = Date.now();
    console.log("epoch and now =")
    console.log(epochDate)
    console.log(now)
    if(epochDate <= now){
        epochDate = now + 604800000;
    } 
    var releaseDate = new Date(epochDate).toISOString();
    console.log(releaseDate);
    
    const payload = {
        Id: data,
        date: new Date(epochDate).toISOString()
    }
    return await axios.post(endpoint, payload, opt)
        .then((result)=> result.data)
        .catch((error) => {
            console.log('error in axios post catch %j', error.response.data)
            throw error
        })
}
  • What makes you think it isn't executing that line? Is it that you don't see a console log line for it? Do you see any errors? – Ken Wayne VanderLinde Apr 09 '21 at 21:34
  • It is unlikely an arbitrary chunk of code is just being skipped. – Dave Newton Apr 09 '21 at 21:37
  • @KenWayneVanderLinde please check it I have updated question – someonetonobody Apr 09 '21 at 21:43
  • So the code clearly runs, otherwise there would be no `releaseDate` to log! You clearly have a logic error in how you're handling dates. I'm no expert with JS dates, but I find it mighty suspicious that you are comparing/manipulating a mix of dates, date times, and integers. I expect that your condition `epochDate <= now` is never true, therefore `epochDate` is never set to 7 days beyond `now`. – Ken Wayne VanderLinde Apr 09 '21 at 21:54
  • You need to define what "*… not geting correct time*" means exactly. Reduce the code to just the part that is not working correctly, provide sample input and actual plus expected output. Adding 7 days in ms (67480000) may not add exactly 7 days since not all days are 24 hours long where daylight saving is observed, see [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date) – RobG Apr 10 '21 at 04:45

1 Answers1

0

You have declared function schedule() as async meaning that it must return a promise that will be fulfilled. At the end of your function, you are returning the promise made by axios but you are also attaching then and catch functions to that promise which is likely producing very confusing results. If I was doing it, I would declare schedule() as you have but make its implementation specifically a promise. Within that promise, you would invoke axios() and, in its then and/or catch, you would call either the accept handler or the reject handler.

Jon Trauntvein
  • 4,453
  • 6
  • 39
  • 69
  • ohh, I am practicing this for quite some time now and helps most of the time. I am having issue with the part where I am converting epoch time to iso format. – someonetonobody Apr 09 '21 at 21:57