【问题标题】:How to get this format of date 2018-05-23T23:00:00.000+00:00? [duplicate]如何获得日期 2018-05-23T23:00:00.000+00:00 的这种格式? [复制]
【发布时间】:2019-12-02 05:00:29
【问题描述】:

我正在尝试按日期过滤一些信息,并且在我的数据库中,日期以这种格式保存:

2018-05-23T23:00:00.000+00:00

这就是我尝试做的事情

router.get('/byDate', function (req, res) {
    req.query.fromDate
    req.query.toDate

    Schedule.find({ date : { 
        $lt: new Date(req.query.toDate).toISOString(), 
        $gte: new Date(req.query.fromDate).toISOString()
      } } , function(err, data){
        if(err) throw err;
        res.json(data);
    });

});     

【问题讨论】:

    标签: javascript angular mongoose


    【解决方案1】:

    您可以使用 JavaScript 日期参考从您的时间戳中获取信息。创建一个日期对象并传递时间戳,您可以使用日期对象提供的许多属性。

    更多信息请查看:https://www.w3schools.com/jsref/jsref_obj_date.asp

    希望这会有所帮助。

    router.get('/byDate', function (req, res) {
        req.query.fromDate
        req.query.toDate
    
        let fromdate = new Date(Date.parse(req.query.fromDate));
    
        let month = new Array();
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";
    
        console.log('Day --->' + fromdate.getDay());
        console.log('Month -->' + month[fromdate.getMonth()]);
        console.log('Year -->' + fromdate.getFullYear());
    
    });  
    

    【讨论】:

      【解决方案2】:

      您指定的格式可以通过将日期转换为 ISO 字符串来获得。但是,您需要执行更多操作才能获得这种特定的所需格式。

      例如:我试过这个。 this.date = new Date(); this.formattedDate = this.date.toISOString();

      输出是 Wed Jul 24 2019 12:08:15 GMT+0530 (India Standard Time) 2019-07-24T06:38:15.837Z

      您可以通过将时间传递给 datetime 管道并指定格式来显示时间。如果您想对其执行任何其他操作,您可以使用另一个答案中提到的链接 (https://www.w3schools.com/jsref/jsref_obj_date.asp) 对此对象执行此操作。

      【讨论】:

        【解决方案3】:

        好吧,我试着像那样切它,它奏效了

         $lt: new Date(req.query.toDate).toISOString().slice(0,21), 
         $gte: new Date(req.query.fromDate).toISOString().slice(0,21)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多