Formatting ISODate from Mongodb

后端 未结 4 1995
离开以前
离开以前 2020-11-30 01:23

In Mongodb I am storing date and time in ISODate format.

Which looks like this

ISODate(\"2012-07-14T01:00:00+01:00\")

Using nodejs

相关标签:
4条回答
  • 2020-11-30 01:44

    you can use mongo query like this yearMonthDayhms: { $dateToString: { format: "%Y-%m-%d-%H-%M-%S", date: {$subtract:["$cdt",14400000]}}}

    HourMinute: { $dateToString: { format: "%H-%M-%S", date: {$subtract:["$cdt",14400000]}}}

    0 讨论(0)
  • 2020-11-30 01:56

    MongoDB's ISODate() is just a helper function that wraps a JavaScript date object and makes it easier to work with ISO date strings.

    You can still use all of the same methods as working with a normal JS Date, such as:

    ISODate("2012-07-14T01:00:00+01:00").toLocaleTimeString()
    
    // Note that getHours() and getMinutes() do not include leading 0s for single digit #s
    ISODate("2012-07-14T01:00:00+01:00").getHours()
    ISODate("2012-07-14T01:00:00+01:00").getMinutes()
    
    0 讨论(0)
  • 2020-11-30 02:04
    // from MongoDate object to Javascript Date object
    
    var MongoDate = {sec: 1493016016, usec: 650000};
    var dt = new Date("1970-01-01T00:00:00+00:00");
        dt.setSeconds(MongoDate.sec);
    
    0 讨论(0)
  • 2020-11-30 02:06

    JavaScript's Date object supports the ISO date format, so as long as you have access to the date string, you can do something like this:

    > foo = new Date("2012-07-14T01:00:00+01:00")
    Sat, 14 Jul 2012 00:00:00 GMT
    > foo.toTimeString()
    '17:00:00 GMT-0700 (MST)'
    

    If you want the time string without the seconds and the time zone then you can call the getHours() and getMinutes() methods on the Date object and format the time yourself.

    0 讨论(0)
提交回复
热议问题