0
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 Answers2

0

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.

RobG
  • 142,382
  • 31
  • 172
  • 209
-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}/

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 16 '23 at 18:38