Categories
Uncategorized

Pure JavaScript to format Date object to ISO String Date only

When working on Mongo Shell, I need to format currentDate to a ISODateĀ  format String YYYY-MM-DD

var formatDate = function (date) {
day = date.getDate();
reportdate = date.getFullYear();
month = date.getMonth() + 1;

if (month < 10) {
reportdate = reportdate + “-0” + month;
} else {
reportdate = reportdate + “-” + month;
}

if (day < 10) {
reportdate = reportdate + “-0” + day;
} else {
reportdate = reportdate + “-” + day;
}
return reportdate;
}
var date = new ISODate(“2021-01-01T05:00:00.000Z”);

print(formatDate(date));

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.