Change Date Format in MongoDB



We have the following date −

01-10-2019

To change the date format, let us use custom variable and convert date to a string and change its format −

Following is the query to implement date to string −

> var inputDate="01-10-2019";
> var formatDate= inputDate.split(/-|\//);
> var outputString= formatDate[2]+'-'+formatDate[0]+'-'+formatDate[1];

Displaying the variable value −

> print(outputString);

This will produce the following output −

2019-01-10
Updated on: 2020-05-13T05:42:17+05:30

425 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements