How To Extract Year, Month, Day, Hour And Minutes From a Date Time Field?

In this post, we will get to know How to extract the year, month, day, hour, and minutes from a Date Time Field where we need to use different methods to get different entities like for example ‘day of the month’, ‘year’, ‘month’, ‘hour’, and ‘minute’ aggregate operator as per the requirements.

Extract

Extract Different Entities From Date And Time

As there are different ways to get different entities of a date and time where we can use different operators of mongoDB to get different entities of the date and time given by some other variable and for the same, we have given some separate examples to explain and get it done we needed. As for each entity, we have separate operators so we have explained all of them one by one here to get the concept as the main output so follow the example given below.

To Get Day

We use the day() function to get the day out of the date so for example look at the example given below and try to understand how it helps in getting the desired result and helps in execution as per our choice.

Take an example of a date that has the data stored as “%dd %mm %yyyy %HH %MM” where we have dd as the day of the month, So it will simply get the result as dd as the day in between 1-31 depending upon the month.

Month

As we have seen in the case of the day we get the number as the day of the month similarly we can get the result as the month of the date we have using the month() function and as mm we will get the month from the date we provided as the example shown the date will be stored in same formate as in case of day So, It also stores the month also so it will result into the number of the month from 1 – 12. to Get the clear idea have a look at the example given below.

Year

Similarly, As we have got the month and day from the date we had provided in a similar way we can also get the year from the date which is stored as yyyy in the example we had taken above so it will help us in getting the exact date there in the given date. And for the same, we use the year() function and we could get our desired result as we want to get to know more and see the implementation have a look at the end below and keep learning new things.

Hour

Similarly to the date, month, and year we can also get the hour of the time which will be of the form HH from the time we stored. Here we use the function hour() to get the hour.

Minute

And As we get the hour of the time we can also get the minute of the time at the moment in the form of MM, By using the function minute().

Here is the example and the implementable code:

date_t.day()
date_t.month()
date_t.year()
date_t.hour()
date_t.minute()

 

 

To learn more about How to extract the year, month, day, hour, and minutes from a DateTimeField visit:  by stack over flow

To learn more about MongoDB Solutions and Tutorials along with the concepts of MongoDB and learn through the best visit: MongoDB Problems And Tutorials.

 

Leave a Comment

%d bloggers like this: