Q8root
Q8root

Reputation: 1355

Javascript Converting a date to millisecond

I am building a counter down . I have the following code:-

var date = new Date(res.data.created_at);
console.log(date); //Sat Jun 20 2020 23:52:05 GMT+0300 (Arabian Standard Time)
date.setDate(date.getDate() + 3); 
console.log(date); //Tue Jun 23 2020 23:52:05 GMT+0300 (Arabian Standard Time)
console.log(date.getMilliseconds()); //0
console.log(date.getTime()); //1592945525000

The counter library i am using is accepting a millisecond as initial count down value. In the above code i want to get a date from api then add 3 days to it, after that get the milliseconds value from now date time to the 3 days added value.

Upvotes: 1

Views: 64

Answers (1)

blex
blex

Reputation: 25634

You can use Date.now() to get the current timestamp, and just do a subtraction:

var endDate = new Date(res.data.created_at);
endDate.setDate(endDate.getDate() + 3);

var diffInMilliseconds = endDate.getTime() - Date.now();

Upvotes: 2

Related Questions