Format the inserted date in MongoDB - mongodb

how can we format the date when you insert it into a collection in MongoDB ?
db.collection('transactions').insertOne({
amount: 1,
id: req.params.userid,
date: new Date ()
})
I tried things like
new Date (<YYYY-mm-dd>)
new Date ("<YYYY-mm-dd>")
But it doesn't work.
Google isn't really helping either.
Thank you

It is not possible with Date type
Reference
new Date() returns the current date as a Date object. mongosh wraps the Date object with the ISODate helper. The ISODate is in
UTC
BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). This results in a representable date range of about 290 million years into the past and future.
On the other hand, you could do the below things
You can either use String type, but you need to convert to date before doing any queries
or
You need to convert to the desired format before returning to the client.

Related

Convert [HH:MM AM/PM] String to ISO Date in Mongo DB

I have two string values which contains Start Time and End Time. They are in following format:
StartTime 07:00 AM
EndTime 10:00 PM
I need to check whether Current Date Time is within these Start and End times.
Therefore, I need to convert above two strings to ISO datetime.
What is the best way to convert [HH:MM AM/PM] string in to ISO date time in Mongo DB?

Is there a way we can get date alone from a datetime field stored as string in MongoDB. We are using Spring MongoDB driver

Let's say I have an date attribute stored in the format dd/MM/YYYY HH:mm:ss i.e. 22/12/2021 10:15:23. Now I just want 22/12/2021 as a date value so that I can do date comparisons. I want this operation to happen at database itself so that I can do date comparisons as part of the query/aggregation itself. Please note I am using Spring MongoDB driver.
Getting date alone from a datetime string is not possible in MongoDB since it always returns an ISO date with time. As a workaround we can convert the time HH:mm:ss to 00:00:00 either by applying String operations on the string attribute or by using Arithmetic operations on the date. We will have to add a temporary field in the query which will hold the converted value using add field operations. And then we can compare them with a given date.

Turning a localized Date to a Timestamp in Firebase

I'm new to Firestore and since it doesn't seem like it has a native createdAt/updatedAt as part of a document, I'm creating them when I create the new document. Using a straight up Date() as the value for my initial dictionary that I save to Firestore obviously gives me a localized date/time – UTC -500, for example.
Firestore stores this as November 19, 2018 at 5:14:54 PM UTC-5
Is there a specific date format that Firestore likes in order to save something as a Timestamp and so that I will be able to sort on it later?
I tried using "yyyy-MM-dd HH:mm:ss Z" as the dateFormat from just printing out the value of Date() in a Playground. Would love some help here. Thanks!
A Timestamp object doesn't have a date format. It's just a measurement of a number of seconds since Unix epoch, plus some number of nanoseconds. It does not accept a formatted time. If you have a formatted time, you will have to parse it to get a Timestamp object in return.`
The date format you're seeing in the console is just the way the console is choosing to format it. If you want to format a Timestamp for display, you'll need to do that yourself.

Date stored in different format

I have this date "2018-05-30T16:19:58.016Z" coming from my Angular app.
In Spring, the field date is as follows :
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;
The date is well stored, but with this format YYYY-MM-dd.
Is there anything that I'm missing ?
MySql date type can't hold data with timestamp. It has to be datetime in order to contain date time with timestamp data.
You probably have to specify the date format going out to the storage, as #JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") only specifies the date format for parsing the date into the Date object. Even if your Date has all of the seconds and timezone information, the default Date toString() is still
Formats a date in the date escape format yyyy-mm-dd.
according to the Java 8 docs. So if you are using that, it would most likely drop all that extra information on conversion.
You can look at this Convert java.util.Date to String for information on how to get a Date to a formatted String.

How to store Indian date and Time in MongoDB ? and How to query using Time fileds(HH:MM:SS)

What is the best way to store indian date and time in mongodb?
I'm going to upload bill details in mongodb. So i've to capture the Bill time which is printed in Bill. This will be in the format like '2014-12-22 14:10:25'. ISODate is good solution? How to covert above date value into ISODate format? Is there any default fuction avilable in mongodb?
How to query the documents based on time elements.For example hourly wise document search.
Please advice
that is possible through JavaScript's Date objec, that supports the ISO date format, so as long as you have access to the date string, you can do something like this:
> doo = new Date("2012-07-14T01:00:00+01:00")
Sat, 14 Jul 2012 00:00:00 GMT
> doo.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.
if any problem please comment me..)