mongodb Get the difference of days from dates having different years - mongodb

I am trying get the difference of days between two dates having different years. Like difference of days between 2015-12-26 and 2016-05-16.
In SQL I would get this below code.
DECLARE #s DATE ='2015-12-26',#t DATE ='2016-05-16'
SELECT DATEDIFF(N,#s,#d)
Can someone help me to convert this SQL Code in mongodb?

Since mongodb gives the difference in milliseconds, I have calculated the milliseconds for each day and divided with difference of milliseconds between two dates. See below example for reference:
>var s =ISODate("1996-12-31"), t=ISODate(), diff = Math.round((t-s)/(1000 * 3600 * 24))
>print(diff)
7140

Related

How to get network days by subtracting from current date and older date on Jasper soft studio? [duplicate]

This question already has answers here:
Calculating Time and Date difference
(1 answer)
How to create a single expression displaying time difference between two Date's as years, months, days, hours, minutes, seconds
(1 answer)
Closed 2 years ago.
I need to find net workdays by subtracting from older date from today ?
Ex:Today Date is 7/14/2020 and older date is some 7/14/2019.
Expected result = 365.
The expression is extract(days from now() - <older date column>).
My recommendation is to have the database server perform this calculation for you by adding the expression to the select list in your query. If you prefer to do this with scripting after Jaspersoft has retrieved the data, then I cannot help you there.

RethinkDB Filter for range of dates and times

My database table has the timestamp format "2019-12-08T13:03:16.502639-0600". I am having difficult figuring out how to filter a 24 hour range of dates so I only get the data plucked from inside that 24 hour running period of time. I need something like now() then back 24 hours on a running basis. I have tried several ways but none of them work.
Do I need to parse the timestamp first and only use the parts I need?
In Python, the query could look like this:
r.table('MyDB').filter(r.row['timestamp'] > r.now() - 24*60*60).run(con)

inconsistency between month, day, second representation of interval data type

I understand why postgresql uses month,day and second fields to representate the sql interval datatype. A month is not always the same length and a day can have 23, 24 or 25 hours if a daylight savings time adjustment is involved. this is from postgresql documentation.
But I then do not understand why this is not consequently handled both for months and days. see the following query which calculates an exact interval where the number of seconds between two points in time is exactly calculatable:
select ('2017-01-01'::timestamp-'2016-01-01'::timestamp); -->366 days.
postgresql chooses to give a result in days. not in months and not in seconds.
But why is the result days and not seconds? it is NOT defined how long days are (they can be 23,24 or 25 hours long). so why does he not give output in seconds?
Then since the length of months is also not defined, why doesn't postgresql give an output of 12 month instead of 366 days?
He does not care that the length of days is not defined, but obviously he cares that the length of month is not defined.
Why this asymmetrie?
For further explanation, see this query:
select ('10 days'::interval-'24 hours'::interval); --> 10 days -24:00:00
you see that postgresql correctly refuses to answer with 9 days. He is pretty aware of the problem that days and hours cannot be interchanged. But then again why does the first query return days?
I can't answer your question, but I think I can point you in the right direction. I think the book SQL-99 Complete, Really is the most accessible source for understanding SQL intervals. It's available online: https://mariadb.com/kb/en/sql-99/08-temporal-values/.
SQL standards describe two kinds of intervals: year-month intervals and day-time intervals. It does this to prevent month parts and day parts from appearing in the same interval, because, as you already know, the number of days in a month is ambiguous. The number of days in the interval '3' month depends on which three months you're talking about.
I think this is the verbose, standard SQL way to write your first query.
select cast(timestamp '2017-01-01' - timestamp '2016-01-01' as interval day to hour) as new_column;
new_column
interval day to hour
--
366 days
I suspect that you'll find that SQL standards have rules for what a SQL dbms is supposed to do when things like interval day to hour are omitted. PostgreSQL might or might not follow those rules.
postgresql chooses to give a result in days. not in months and not in seconds.
Standard SQL prevents month parts and day parts from appearing in the same interval. Also, the range of valid seconds is from 0 to 59.
select interval '59' second;
interval
interval second
--
00:00:59
select interval '60' second;
interval
interval second
--
00:01:00

Hive: Subtracting 1 year from current date

I'm trying to find the best way to subtract 1 year and also one month from the current date in a Hive query. Using the following, I don't believe it will take into account leap years or if the fact that months have different amounts of days so eventually the code will break. Any help would be greatly appreciated!
set my_date = from_unixtime(unix_timestamp()-365*60*60*24, 'yyyy-MM-dd');
set my_date = from_unixtime(unix_timestamp()-30*60*60*24, 'yyyy-MM-dd');
Thank!
-Rebecca
If you have date format like yyyy-MM-dd hh:mm:ss in Hive, it is easy to implement using following functions
concat((year(date_field)-1),'-', (month(date_field)-1), '-', day(date_field))
Use IF and CASE functions to implement your logic to find whether it is a leap year or not(by dividing year by 4)

how to obtain interval from date on postgres in days

I have two columns, say "2010-10-26" and "2010-08-23" and I would like to obtain the interval in days, what is the best way to do this on a select query taking into account that there are months with 30,31,29,28 days? Is there any function that already does this?
Thank you.
If they are already in the date format, it's very easy:
SELECT '10/26/2010'::date - '08/23/2010'::date;
OR:
SELECT date '2001-10-01' - date '2001-09-28'; // outputs integer 3