Reputation:
I am trying to get current time and the time for twenty minutes later in certain format using JavaScript. The desired output should be in this format:
30/09/2017 22:11:23
dd/mm/yyyy hh:MM:ss
Here is my code:
//...
The output I have obtained:
30/09/2017 22:1506781883422:23
30/09/2017 22:11:23
The twenty minutes later one seemed wrong.
Upvotes: 0
Views: 56
Reputation: 4044
Try this:
var now = new Date();
var in20 = new Date(now.getTime() + (1000*60*20));// add 20 minutes
console.log(now.toLocaleDateString() + " " + now.toLocaleTimeString());
console.log(in20.toLocaleDateString() + " " + in20.toLocaleTimeString());
Upvotes: 2
Reputation: 692
There is much easier and faster way:
let currentTime = new Date;
let twentyMinutesLater = new Date;
twentyMinutesLater.setMinutes(twentyMinutesLater.getMinutes()+20);
console.log(currentTime.toString())
console.log(twentyMinutesLater.toString())
If you need to format the string, use the different toString() functions, provided by the Date class.
Upvotes: 1