Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
0e02203b
Commit
0e02203b
authored
Oct 16, 2020
by
mlunoe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feat(datetime utility): add diff months util
Add utility to get difference in months
parent
14c0ac9a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
app/assets/javascripts/lib/utils/datetime_utility.js
app/assets/javascripts/lib/utils/datetime_utility.js
+15
-0
spec/frontend/lib/utils/datetime_utility_spec.js
spec/frontend/lib/utils/datetime_utility_spec.js
+14
-0
No files found.
app/assets/javascripts/lib/utils/datetime_utility.js
View file @
0e02203b
...
...
@@ -744,6 +744,21 @@ export const differenceInSeconds = (startDate, endDate) => {
return
(
endDate
.
getTime
()
-
startDate
.
getTime
())
/
1000
;
};
/**
* A utility function which computes the difference in months
* between 2 dates.
*
* @param {Date} startDate the start date
* @param {Date} endDate the end date
*
* @return {Int} the difference in months
*/
export
const
differenceInMonths
=
(
startDate
,
endDate
)
=>
{
const
yearDiff
=
endDate
.
getYear
()
-
startDate
.
getYear
();
const
monthDiff
=
endDate
.
getMonth
()
-
startDate
.
getMonth
();
return
monthDiff
+
12
*
yearDiff
;
};
/**
* A utility function which computes the difference in milliseconds
* between 2 dates.
...
...
spec/frontend/lib/utils/datetime_utility_spec.js
View file @
0e02203b
...
...
@@ -682,6 +682,20 @@ describe('differenceInSeconds', () => {
});
});
describe
(
'
differenceInMonths
'
,
()
=>
{
const
startDateTime
=
new
Date
(
'
2019-07-17T00:00:00.000Z
'
);
it
.
each
`
startDate | endDate | expected
${
startDateTime
}
|
${
startDateTime
}
|
${
0
}
${
startDateTime
}
|
${
new
Date
(
'
2019-12-17T12:00:00.000Z
'
)}
|
${
5
}
${
startDateTime
}
|
${
new
Date
(
'
2021-02-18T00:00:00.000Z
'
)}
|
${
19
}
${
new
Date
(
'
2021-02-18T00:00:00.000Z
'
)}
|
${
startDateTime
}
|
${
-
19
}
`
(
'
returns $expected for $endDate - $startDate
'
,
({
startDate
,
endDate
,
expected
})
=>
{
expect
(
datetimeUtility
.
differenceInMonths
(
startDate
,
endDate
)).
toBe
(
expected
);
});
});
describe
(
'
differenceInMilliseconds
'
,
()
=>
{
const
startDateTime
=
new
Date
(
'
2019-07-17T00:00:00.000Z
'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment