0

I am using the following date method to insert a date in mongo DB:

BasicDBObject Created_Date = new BasicDBObject("date", now);
record.put("Created_Date", now);

It returns the result in below format:

 "Created_Date" : ISODate("2017-10-11T06:16:10.536Z")

Is it possible to insert date without time zone i.e.

"Created_Date" :"2017-10-11"

Thanks for any help.

Víctor López
  • 819
  • 1
  • 7
  • 19
  • @glitch "MongoDB will not be able to tell whether `2017-10-11` is greater than `2017-10-10`." - Why not? Order of dates corresponds to order of strings. – lexicore Oct 11 '17 at 07:47
  • Why don't you just convert date to string prior to saving? – lexicore Oct 11 '17 at 07:49
  • @glitch: thanks but I want to insert current date and not the particular date like "2017-10-11" using ("Created_Date", now). – Spriha Srivastava Oct 11 '17 at 08:37
  • Possible duplicate of [MongoDB - Storing date without timezone](https://stackoverflow.com/questions/38401231/mongodb-storing-date-without-timezone) – Clement Amarnath Oct 11 '17 at 08:45

1 Answers1

3

Your question is not consistent with your example. Your example shows storing the date without the time portion of the date, but your question is about storing the date without the timezone.

MongoDB doesn't store timezone as part of the date, all dates are stored in UTC. See Model Time Data for more details.

If you want to store the date without the time (as you show in your example), then "truncate" the date. You will still have a full Date object, with the time appearing as midnight UTC, but that's a fairly typical way of handling dates as you describe.

Since you're using Java, here are a few ways to truncate the date : Java Date cut off time information

helmy
  • 9,068
  • 3
  • 32
  • 31
  • even if I truncate the time to 00:00:00 still it converts that time to UTC time and stores in mongo – Soumitri Pattnaik Jun 19 '20 at 06:41
  • As I mentioned, you will still have a full Date object, with the time appearing as midnight UTC. The right thing to do is to properly format the dates in your application to show only the relevant portion. Furthermore, typically you would want to format all dates (regardless of if time is included) based on the User's timezone, since what constitutes a given "day" is dependent on your perspective of a given timezone. – helmy Jun 19 '20 at 18:11