Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timezone in mongo query

I have data inserted in UTC time format in mongodb. I want timings to be converted based on the timezone. Is there any possibility to do so in mongo query?

like image 204
user3702039 Avatar asked Jun 23 '15 06:06

user3702039


People also ask

Does MongoDB have timestamp?

The MongoDB timestamp looks similar to Date data type, where it has the same 64-bit value. But it also has a few aspects where it differs from the Date. The MongoDB Timestamp is quite used for the internal purpose, and with every single instance of mongodb, the values generated for timestamp are unique.

What is the datetime format in MongoDB?

new Date("<YYYY-mm-ddTHH:MM:ssZ>") specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC.

What format are MongoDB timestamps?

Timestamps. BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. This internal timestamp type is a 64 bit value where: the most significant 32 bits are a time_t value (seconds since the Unix epoch)

What is ISO Date in MongoDB?

ISODate("2012-12-19T06:01:17.171Z") ISODate() is a helper function that's built into to MongoDB and wraps the native JavaScript Date object. When you use the ISODate() constructor from the Mongo shell, it actually returns a JavaScript Date object.


2 Answers

In mongo version 3.6 timezone has been added, mongo doc

expression to extract date part with timezone is

{ date: <dateExpression>, timezone: <tzExpression> }

we can either specify the timezone or offset while getting the date parts.

see my answer posted here

to get date from date with timezone America/Chicago

{ $month: {
    date: new Date(),
    timezone: "America/Chicago"
} }

or with offset

{ $month: {
    date: ISODate(),
    timezone: "-0500"
} }
like image 180
Saravana Avatar answered Sep 18 '22 23:09

Saravana


Let consider your document contains ISODate as below :

db.collection.insert({"date":new Date()})

Above query insert date in ISODate format now you want to convert this ISODate into give timeZone.

Suppose you want to convert above date to Eastern Daylight Saving Time ( EDT ) epoch time zone conertor then offset converted as 14400 * 1000. First convert ISODate to timeStamp and then use substract EDT OffsetintimeStampand then converttimeStamptoISODate` again.

Check below aggregation query :

db.collection.aggregate({
  "$project": {
    "timestamp": { //convert ISODate tom timestamp
      "$subtract": [{
        "$divide": [{
          "$subtract": ["$date", new Date("1970-01-01")]
        }, 1000]
      }, {
        "$mod": [{
          "$divide": [{
            "$subtract": ["$date", new Date("1970-01-01")]
          }, 1000]
        }, 1]
      }]
    }
  }
}, {
  "$project": {
    "timeZoneTimeStamp": {
      "$subtract": [{ //substract timestamp to given offset if offset will in postive then replace  subtract  to add
        "$multiply": ["$timestamp", 1000]
      }, 14400000]
    }
  }
}, {
  "$project": {
    "timeZoneTimeStamp": 1, //converted timeZoneTimeStamp if required 
    "_id": 0,
    "newDate": { // newDate is converted timezone ISODate
      "$add": [new Date(0), "$timeZoneTimeStamp"]
    }
  }
})

NOTE : In above query conversion from ISODATE to timeStamp ref. here

like image 45
Yogesh Avatar answered Sep 21 '22 23:09

Yogesh