Skip to content

Add week support #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions moment-precise-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ if (typeof moment === "undefined" && typeof require === 'function') {
years: 'years',
month: 'month',
months: 'months',
week: 'week',
weeks: 'weeks',
day: 'day',
days: 'days',
hour: 'hour',
Expand All @@ -24,7 +26,7 @@ if (typeof moment === "undefined" && typeof require === 'function') {
return num + ' ' + STRINGS[word + (num === 1 ? '' : 's')];
}

function buildStringFromValues(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff){
function buildStringFromValues(yDiff, mDiff, wDiff, dDiff, hourDiff, minDiff, secDiff){
var result = [];

if (yDiff) {
Expand All @@ -33,6 +35,9 @@ if (typeof moment === "undefined" && typeof require === 'function') {
if (mDiff) {
result.push(pluralize(mDiff, 'month'));
}
if (wDiff) {
result.push(pluralize(wDiff, 'week'));
}
if (dDiff) {
result.push(pluralize(dDiff, 'day'));
}
Expand All @@ -49,11 +54,12 @@ if (typeof moment === "undefined" && typeof require === 'function') {
return result.join(STRINGS.delimiter);
}

function buildValueObject(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff, firstDateWasLater) {
function buildValueObject(yDiff, mDiff, wDiff, dDiff, hourDiff, minDiff, secDiff, firstDateWasLater) {
return {
"years" : yDiff,
"months" : mDiff,
"days" : dDiff,
"weeks" : wDiff,
"hours" : hourDiff,
"minutes" : minDiff,
"seconds" : secDiff,
Expand All @@ -66,9 +72,9 @@ if (typeof moment === "undefined" && typeof require === 'function') {

moment.preciseDiff = function(d1, d2, returnValueObject) {
var m1 = moment(d1), m2 = moment(d2), firstDateWasLater;

m1.add(m2.utcOffset() - m1.utcOffset(), 'minutes'); // shift timezone of m1 to m2

if (m1.isSame(m2)) {
if (returnValueObject) {
return buildValueObject(0, 0, 0, 0, 0, 0, false);
Expand All @@ -87,6 +93,7 @@ if (typeof moment === "undefined" && typeof require === 'function') {

var yDiff = m2.year() - m1.year();
var mDiff = m2.month() - m1.month();
var wDiff = 0;
var dDiff = m2.date() - m1.date();
var hourDiff = m2.hour() - m1.hour();
var minDiff = m2.minute() - m1.minute();
Expand All @@ -113,15 +120,19 @@ if (typeof moment === "undefined" && typeof require === 'function') {
}
mDiff--;
}
if (dDiff > 7) {
wDiff = Math.floor(dDiff / 7);
dDiff = dDiff % 7;
}
if (mDiff < 0) {
mDiff = 12 + mDiff;
yDiff--;
}

if (returnValueObject) {
return buildValueObject(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff, firstDateWasLater);
return buildValueObject(yDiff, mDiff, wDiff, dDiff, hourDiff, minDiff, secDiff, firstDateWasLater);
} else {
return buildStringFromValues(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff);
return buildStringFromValues(yDiff, mDiff, wDiff, dDiff, hourDiff, minDiff, secDiff);
}


Expand Down