back to Overview

_.dateDiff()

Calculates the time difference between both dates, using in the unit determined by the property.
Util module only.

Syntax

_.dateDiff(property, date1, date2)

Parameters

date1
the first Date
date2
the first Date
property
a property name to represent the unit of the value. Can be 'fullYear', 'month', 'date', 'hours', 'minutes', 'seconds' or 'milliseconds'.
(return value)
the time difference between the two dates. Negative if date2 is earlier than date1.

Description

Calculates the time difference between both dates, using in the unit determined by the property.

If the unit is not calendar-based ('hours', 'minutes', 'seconds' or 'milliseconds') the result is calculated with full precision and not rounded. If the unit is calendar-based ('fullYear', 'month', 'date'), the result is the amount of full units between those dates in the current time zone.

If date2 is earlier than date1, the result is negative.

Example

Calculate duration between two dates:

function diff(d1, d2) {
    return _.dateDiff('fullYears', d1, d2) + ' years,' +
           _.dateDiff('months', d1, d2) + ' months and' +
           _.dateDiff('date', d1, d2) + ' days';
}

Comments

comments powered by Disqus

back to Overview

Functions