How to subtract 2 dates on momentjs - momentjs

Hi im currently using momentjs for my dates in my project and im having a bit of trouble when subtracting 2 dates.
Here are my sample dates:
2016-10-08 10:29:23
2016-10-08 11:06:55
ive tried using the diff and subtract from the guide docs of momentjs but i got nothing.
And what if the date subtracted dates are more than 24 hours?
Thnks in advance.

You are correct, you can use moment's diff function to subtract two dates (see my example on Plunker):
var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-08 11:06:55');
var diff = date2.diff(date1);
Diff will be equal to 2252000, the number of milliseconds between the two date. See documentation for more details.
You can pass a second argument to diff with the measurement to use (years, months, weeks, days, hours, minutes, and seconds), so if you want to know the number of minutes between the two dates you can write:
var diffInMinutes = date2.diff(date1, 'minutes');
And you get 37 minutes.

Related

Dart - Date Differences

I know there's a pre-function differences for DateTime to calculate the duration differences between two DateTime. But I need to calculate the date differences between two DateTime. For example, the differences between 2020-03-01 23:59:59 and 2020-03-02 00:00:01 should return 1. How can I do that? Thanks in advances
If you create another two DateTime objects using only the year , month and day of the months of the previous dates using the 'DateTime' constructor it should work.
Like so :
DateTime date1,date2;//These should be initialized
Duration difference = DateTime(date1.year,date1.month,date1.day).diffrence (DateTime(date2.year,date2.month,date2.day));
print (diffrence.inDays);

I do not get the real value in days, hours and minutes by subtracting two dates

I am new in this library. I have 2 dates, and I would like to obtain the difference in days, minutes and hours between both. but for some reason the output is an erroneous data. I have based on this page
to get the exact difference and it is different to my result. what am I doing wrong?
var now=2018-07-04 00:37:02;
var then=2018-07-05 08:00:00;
moment.utc(moment(now,"YYYY/MM/DD HH:mm:ss").diff(moment(then,"YYYY/MM/DD
HH:mm:ss"))).format("HH:mm:ss");
output is : 16:37
There are two significant problems here:
a.diff(b) returns a - b, so it will be positive if a is later than b. You're using now.diff(then) effectively, so that will currently give a negative difference, and I suspect you're expecting it to be positive.
You're turning a duration in time into a date/time using moment.utc. The value just isn't a date/time, so shouldn't be treated as one. Instead, use moment.duration(...) to convert to a momentjs Duration object, which you can then handle however you want.
Here's a complete example, having broken out each of the conversions involved for readability:
var now = "2018-07-04 00:37:02";
var then = "2018-07-05 08:00:00";
var parsedNow = moment(now, "YYYY/MM/DD HH:mm:ss");
var parsedThen = moment(then, "YYYY/MM/DD HH:mm:ss");
var diff = parsedThen.diff(parsedNow);
var durationDiff = moment.duration(diff);
alert(durationDiff.toISOString());
Output: PT31H22M58S
That 31 hours is 1 day + 7 hours etc. While a duration has the concept of days, months, years etc, I don't see any way of normalizing a duration created from diff to "1 day 7 hours". (It could certainly be done, but only as far as days - you couldn't normalize to include months without providing more context.)

Is IDL able to add / subtract from date?

As you can see the question above, I was wondering if IDL is able to add or subtract days / months / years to a given date.
For example:
given_date = anytim('01-jan-2000')
print, given_date
1-Jan-2000 00:00:00.000
When I would add 2 weeks to the given_date, then this date should appear:
15-Jan-2000 00:00:00.000
I was already looking for a solution for this problem, but I unfortunately couldn't find any solution.
Note:
I am using a normal calendar date, not the julian date.
Are you only concerned with dates after 1582? Is accuracy to the second important?
The ANYTIM routine is not part of the IDL distribution. Possibly there are third party routines to handle time increments, but I don't know of any builtin to the IDL library.
By default, which you are using, ANYTIM returns seconds from Jan 1, 1979. So to add/subtract some number of days, weeks, or years, you could calculate the number of seconds in the time interval. Of course, this does not take into account leap seconds/years (but leap years are fairly easy to take into account, leap seconds requires a database of when they were added). And adding months is going to require determining which month so to determine the number of days in it.
IDL can convert to and from Julian dates using JULDAY and CALDAT.
You may also read and write Julian dates (which are doubles or long integers) to and from strings using the format keyword to PRINT, STRING, and READS.
You'll want to use the (C()) calendar date format code.
format='(c(cdi0,"-",cMoa,"-"cyi04," ",cHi02,":",cmi02,":",csf06.3))'
date = julday(1, 1, 2000)
print, date, format=format
; 1-Jan-2000 00:00:00.000
date = date + 14
print, date, format=format
; 15-Jan-2000 00:00:00.000

Calculate Date Duration

I have to calculate the duration of two dates with days and hours remaining.
If the two dates have different days, I need to return the duration in days and hours.
For example, given the following input:
2016-12-11T09:30:00.000Z and 2016-12-12T11:30:00.000Z
I would like to have this output:
1 day 2 hrs
How to achieve this using moment.js?
You can use moment-duration-format plug-in.
Just create moment object from your strings/dates, then get the difference in milliseconds using diff method in order to create a duration object. Use format method from moment-duration-format to print duration according your needs.
Here a working example:
// Create moment objects
var m1 = moment('2016-12-11T09:30:00.000Z');
var m2 = moment('2016-12-12T11:30:00.000Z');
// Get the difference in milliseconds
var diff = Math.abs( m1.diff(m2) );
// Format duration according your needs
console.log(moment.duration(diff).format("d [day] h [hrs]"));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>

XQuery - How to get 90 days before date from a given date?

In xquery, How to get 90 days before date from a given date?
Say given date is 30-03-2012.
I want to get the date which is 90 days before the given date. Leap year calculation should also not get missed.
I could not find any built in functions. There are add/substract methods, but it works only with two dates.
Any ideas?
You need to subtract a dayTimeDuration (90 days) from a date, i.e.
xs:date("2012-03-30") - xs:dayTimeDuration("P90D")

Resources