In my mean stack app, i have data on a date basis. In angular side, i used a date picker to get/set the date that the read/write of Data to be handled. The date picker produce date of the form "dd-mm-yyyy". What is the easiest way to convert this into mongodb understandable format, and back.
var str = "29-1-2016";
darr = str.split("-"); // ["29", "1", "2016"]
var dobj = new Date(parseInt(darr[2]),parseInt(darr[1])-1,parseInt(darr[0]));
// Date {Fri Jan 29 2016 00:00:00 GMT+0530(utopia standard time)
console.log(dobj.toISOString());
//2016-01-28T18:30:00.000Z
This will do it, but is there an easier way..!!
You could use this solution (worked in my case) -
Firstly, use Moment.js in your code, include it in your project. Now, The time string that you are getting here var str = "29-1-2016";
and along with moment.js use the below code and you're good to go -
var str = "29-1-2016";
var time = moment(str).toISOString();
\\ This variable time is now converted into ISO string
convert your date using toISOString
var mydate="2017-04-24"
var IsDate=new Date(mydate).toISOString();
console.log('my iso date',IsDate);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.