Remove Seconds/ Milliseconds from Date and convert to ISO String?

JavascriptWeb DevelopmentObject Oriented Programming

Let’s get the current date first −

var currentDate = new Date();
console.log("The current date is as follows="+currentDate);

Now, let us remove seconds/ milliseconds by setting them to 0 using setSeconds() −

currentDate.setSeconds(0,0);

Convert to ISO String using toISOString() −

currentDate.toISOString()

Let us now see the complete code with output −

Example

var currentDate = new Date();
console.log("The current date is as follows="+currentDate);
currentDate.setSeconds(0,0);
console.log("After removing seconds from date, the new date is as
follows=");
console.log(currentDate.toISOString());

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo143.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo143.js
The current date is as follows=Sat Aug 01 2020 18:20:09 GMT+0530 (India Standard Time)
After removing seconds from date, the new date is as follows=
2020-08-01T12:50:00.000Z
raja
Published on 11-Sep-2020 08:10:08
Advertisements