skip to Main Content

I think the task is very primitive. But I can’t handle it.
I work online (Excel app).
I have cells with time data (dd.mm.yyyy)
Task "I need to find out the time difference between the current date and the time value in the cell)
enter image description here

I want to use the current time (const start = Date.now()) and the time in the cell (on the selected sheet)

enter image description here

2

Answers


  1. The Error in Your Code : In "01.07.2023" , Date( ) is Treating input as MM-DD-YYYY ;

    // After getting sheet.getRandge(*).getValue();
    cellDate="01.07.2023" 
    
    const [day, month, year] = cellDate.split(".");
    const specificDate = new Date(`${year}-${month}-${day}`);
    
    const differenceInDays=Math.abs(Date.now()-specificDate)/86400000; 
    //1 day=  60 * 60 * 24 seconds =86400 seconds= 86400*1000 Milliseconds
    
    console.log("The Difference - ",parseInt(differenceInDays), " day")
    console.log("//If you want float ",differenceInDays, " in days")
    Login or Signup to reply.
  2. function timeDiff1(Start,End) {
      if(Start && End) {
        var second=1000;
        var minute=60*second;
        var hour=minute*60;
        var day=hour*24;
        var t1=new Date(Start).valueOf();
        var t2=new Date(End).valueOf();
        var d=t2-t1;
        var days=Math.floor(d/day);
        var hours=Math.floor(d%day/hour);
        var minutes=Math.floor(d%day%hour/minute);
        var seconds=Math.floor(d%day%hour%minute/second);
        return 'dd:hh:mm:ssn' + days + ':' + hours + ':' + minutes + ':' + seconds;  
        //return d/day;
        //return hours + minutes/60;
      }
      else
      {
        return 'Invalid Inputs';
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search