skip to Main Content
const seconds=1000, minute=60*seconds,hours= 60*minute,day= 24*hours;
const days= document.querySelector(".days");
const hrs= document.querySelector(".hrs");
const min= document.querySelector(".min");
const sec= document.querySelector(".sec");
//main target


//function
 const timerfunction =()=>{
let dd = prompt("Enter Date").padStart(2,"0");
let mm = prompt("Enter Month").padStart(2,"0");
let yyyy =prompt ("Enter Year") ;
console.log(`${dd}/${mm}/${yyyy}`);
const target = `${dd}/${mm}/${yyyy}`;
console.log(target);
let targetdate = new Date(Date.parse(target)).getTime();
console.log(targetdate);
setInterval(()=>{ 
//get current time
const currentdate = new Date();
var currentdates = currentdate.getTime();
const difference =targetdate-currentdates;  
const differencedays = Math.floor((difference)/day );
const differencehours = Math.floor(((difference)%day)/hours);
const differencemins = Math.floor(((difference)%hours)/minute);
const differenceseconds = Math.floor(((difference)%minute)/seconds);
days.innerText =differencedays;
hrs.innerText =differencehours ;
min.innerText =differencemins ;
sec.innerText =differenceseconds;
console.log(`${differencedays}:${differencehours}:${differencemins}:${differenceseconds}`);
}, 1000);
 }
 timerfunction(); 

what is the problem with targetdate ?
during the counter timer project where i am printing the remaining time on the screen id=f the currentdate stores the current date and targetdate stores the future targeted date .and timerfunction is the function which tells the remaining time.
targetdate is returning NaN , why?

2

Answers


  1. change order in variable target:

    ${yyyy}/${mm}/${dd}/

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

    or try whit

    ${mm}/${dd}/${yyyy}/

    Login or Signup to reply.
  2. Re:

    const target = `${dd}/${mm}/${yyyy}`;
    console.log(target);
    let targetdate = new Date(Date.parse(target)).getTime();
    

    The use of Date.parse in the last line is redundant, the following:

    let targetdate = new Date(target).getTime();
    

    will produce an identical result. The variable name targetdate seems inappropriate given that the value is a time value, not a date. The following will produce an identical result more efficiently:

    let targetdate = Date.parse(target);
    

    The format dd/mm/yyyy is not supported by ECMA-262 so parsing is implementation dependent. Given a pattern d{2}/d{2}/d{4} most (if not all) ECMAScript parsers will guess that the format is mm/dd/yyyy, so will return unexpected results except where the values for month and day are the same (e.g. 08/08/2023).

    Since the values for year, month and day are in separate variables, it would be more efficient and less prone to error to do:

    let targetdate = new Date(yyyy, mm-1, dd).getTime();
    

    The algorithm for getting the difference between two dates will not be accurate in paces where daylight saving is observed since not all days are 24 hours long. A better algorithm is here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search