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
74f6ef9f
Commit
74f6ef9f
authored
Feb 03, 2018
by
Kushal Pandya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more date helper functions
parent
94b1d7ed
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
121 additions
and
2 deletions
+121
-2
app/assets/javascripts/lib/utils/datetime_utility.js
app/assets/javascripts/lib/utils/datetime_utility.js
+121
-2
No files found.
app/assets/javascripts/lib/utils/datetime_utility.js
View file @
74f6ef9f
...
...
@@ -9,6 +9,20 @@ import {
window
.
timeago
=
timeago
;
window
.
dateFormat
=
dateFormat
;
/**
* Returns i18n month names array.
* If `abbreviated` is provided, returns abbreviated
* name.
*
* @param {Boolean} abbreviated
*/
const
getMonthNames
=
(
abbreviated
)
=>
{
if
(
abbreviated
)
{
return
[
s__
(
'
Jan
'
),
s__
(
'
Feb
'
),
s__
(
'
Mar
'
),
s__
(
'
Apr
'
),
s__
(
'
May
'
),
s__
(
'
Jun
'
),
s__
(
'
Jul
'
),
s__
(
'
Aug
'
),
s__
(
'
Sep
'
),
s__
(
'
Oct
'
),
s__
(
'
Nov
'
),
s__
(
'
Dec
'
)];
}
return
[
s__
(
'
January
'
),
s__
(
'
February
'
),
s__
(
'
March
'
),
s__
(
'
April
'
),
s__
(
'
May
'
),
s__
(
'
June
'
),
s__
(
'
July
'
),
s__
(
'
August
'
),
s__
(
'
September
'
),
s__
(
'
October
'
),
s__
(
'
November
'
),
s__
(
'
December
'
)];
};
/**
* Given a date object returns the day of the week in English
* @param {date} date
...
...
@@ -143,7 +157,6 @@ export const getDayDifference = (a, b) => {
* @param {Number} seconds
* @return {String}
*/
// eslint-disable-next-line import/prefer-default-export
export
function
timeIntervalInWords
(
intervalInSeconds
)
{
const
secondsInteger
=
parseInt
(
intervalInSeconds
,
10
);
const
minutes
=
Math
.
floor
(
secondsInteger
/
60
);
...
...
@@ -158,7 +171,7 @@ export function timeIntervalInWords(intervalInSeconds) {
return
text
;
}
export
function
dateInWords
(
date
,
abbreviated
=
false
)
{
export
function
dateInWords
(
date
,
abbreviated
=
false
,
hideYear
=
false
)
{
if
(
!
date
)
return
date
;
const
month
=
date
.
getMonth
();
...
...
@@ -169,9 +182,115 @@ export function dateInWords(date, abbreviated = false) {
const
monthName
=
abbreviated
?
monthNamesAbbr
[
month
]
:
monthNames
[
month
];
if
(
hideYear
)
{
return
`
${
monthName
}
${
date
.
getDate
()}
`
;
}
return
`
${
monthName
}
${
date
.
getDate
()}
,
${
year
}
`
;
}
/**
* Returns month name based on provided date.
*
* @param {Date} date
* @param {Boolean} abbreviated
*/
export
const
monthInWords
=
(
date
,
abbreviated
=
false
)
=>
{
if
(
!
date
)
{
return
''
;
}
return
getMonthNames
(
abbreviated
)[
date
.
getMonth
()];
};
/**
* Returns number of days in a month for provided date.
* courtesy: https://stacko(verflow.com/a/1185804/414749
*
* @param {Date} date
*/
export
const
totalDaysInMonth
=
(
date
)
=>
{
if
(
!
date
)
{
return
0
;
}
return
new
Date
(
date
.
getFullYear
(),
date
.
getMonth
()
+
1
,
0
).
getDate
();
};
/**
* Returns list of Dates referring to Sundays of the month
* based on provided date
*
* @param {Date} date
*/
export
const
getSundays
=
(
date
)
=>
{
if
(
!
date
)
{
return
[];
}
const
daysToSunday
=
[
'
Saturday
'
,
'
Friday
'
,
'
Thursday
'
,
'
Wednesday
'
,
'
Tuesday
'
,
'
Monday
'
,
'
Sunday
'
];
const
month
=
date
.
getMonth
();
const
year
=
date
.
getFullYear
();
const
sundays
=
[];
const
dateOfMonth
=
new
Date
(
year
,
month
,
1
);
while
(
dateOfMonth
.
getMonth
()
===
month
)
{
const
dayName
=
getDayName
(
dateOfMonth
);
if
(
dayName
===
'
Sunday
'
)
{
sundays
.
push
(
new
Date
(
dateOfMonth
.
getTime
()));
}
const
daysUntilNextSunday
=
daysToSunday
.
indexOf
(
dayName
)
+
1
;
dateOfMonth
.
setDate
(
dateOfMonth
.
getDate
()
+
daysUntilNextSunday
);
}
return
sundays
;
};
/**
* Returns list of Dates representing a timeframe of Months from month of provided date (inclusive)
* up to provided length
*
* For eg;
* If current month is January 2018 and `length` provided is `6`
* Then this method will return list of Date objects as follows;
*
* [ October 2017, November 2017, December 2017, January 2018, February 2018, March 2018 ]
*
* If current month is March 2018 and `length` provided is `3`
* Then this method will return list of Date objects as follows;
*
* [ February 2018, March 2018, April 2018 ]
*
* @param {Number} length
* @param {Date} date
*/
export
const
getTimeframeWindow
=
(
length
,
date
)
=>
{
if
(
!
length
)
{
return
[];
}
const
currentDate
=
date
instanceof
Date
?
date
:
new
Date
();
const
currentMonthIndex
=
Math
.
floor
(
length
/
2
);
const
timeframe
=
[];
// Move date object backward to the first month of timeframe
currentDate
.
setDate
(
1
);
currentDate
.
setMonth
(
currentDate
.
getMonth
()
-
currentMonthIndex
);
// Iterate and update date for the size of length
// and push date reference to timeframe list
for
(
let
i
=
0
;
i
<
length
;
i
+=
1
)
{
timeframe
.
push
(
new
Date
(
currentDate
.
getTime
()));
currentDate
.
setMonth
(
currentDate
.
getMonth
()
+
1
);
}
// Change date of last timeframe item to last date of the month
timeframe
[
length
-
1
].
setDate
(
totalDaysInMonth
(
timeframe
[
length
-
1
]));
return
timeframe
;
};
window
.
gl
=
window
.
gl
||
{};
window
.
gl
.
utils
=
{
...(
window
.
gl
.
utils
||
{}),
...
...
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