how to do i get trunc day date [duplicate]

问题: This question already has an answer here: Round a timestamp to the nearest date 6 answers Group result by 15 minutes time interval in MongoDb 5 answer...

问题:

This question already has an answer here:

i want to get the trunc date with such format :

20190326

this code in my schema :

 dateCreated: {
     
        type: Date,
        default: Date.now
    }
gives the following result :

2019-03-26T01:05:43.778Z


回答1:

Use Aggregation $dateToString

db.collection.aggregate([{
  $project: {
    date: {
      $dateToString: {
        format: "%d%m%Y",
        date: "$dateCreated"
      }
    }
  }
}])

You can set formate according to requirement. Sample data here.


回答2:

this solved it :

const date = new Date();
const today = date.getUTCFullYear().toString()+ (date.getUTCMonth()+1)+date.getUTCDate().toString();


export const taskSchema = new Schema ({
dateCreated: {
     
        type: String,
        default: today
    }
});

  • 发表于 2019-03-27 05:06
  • 阅读 ( 166 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除