[英]how can i convert a date like dd-mm-yyyy into ISO date format, to store in a mongoDB
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.在 angular 端,我使用日期选择器来获取/设置要处理的数据读/写的日期。 The date picker produce date of the form "dd-mm-yyyy".日期选择器产生“dd-mm-yyyy”形式的日期。 What is the easiest way to convert this into mongodb understandable format, and back.将其转换为 mongodb 可理解格式并返回的最简单方法是什么。
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.首先,在您的代码中使用Moment.js ,将其包含在您的项目中。 Now, The time string that you are getting here var str = "29-1-2016";
现在,你在这里得到的时间字符串var str = "29-1-2016";
and along with moment.js use the below code and you're good to go -和moment.js 一起使用下面的代码,你很高兴 -
var str = "29-1-2016";
var time = moment(str).toISOString();
\\ This variable time is now converted into ISO string
convert your date using toISOString使用 toISOString 转换您的日期
var mydate="2017-04-24"
var IsDate=new Date(mydate).toISOString();
console.log('my iso date',IsDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.