Commit dc7b952a authored by Michael Lunøe's avatar Michael Lunøe Committed by Jacques Erasmus

Feat(datetime_utility): add formatDateAsMonth

Add function to format a date as a month string
to be used in charts displaying monthly data,
specifically for the instance analytics, but this
should be useble elsewhere.
parent bb0492e4
......@@ -85,6 +85,21 @@ export const getDayName = date =>
__('Saturday'),
][date.getDay()];
/**
* Returns the i18n month name from a given date
* @example
* formatDateAsMonth(new Date('2020-06-28')) -> 'Jun'
* @param {String} datetime where month is extracted from
* @param {Object} options
* @param {Boolean} options.abbreviated whether to use the abbreviated month string, or not
* @return {String} the i18n month name
*/
export function formatDateAsMonth(datetime, options = {}) {
const { abbreviated = true } = options;
const month = new Date(datetime).getMonth();
return getMonthNames(abbreviated)[month];
}
/**
* @example
* dateFormat('2017-12-05','mmm d, yyyy h:MMtt Z' ) -> "Dec 5, 2017 12:00am GMT+0000"
......
......@@ -69,6 +69,34 @@ describe('Date time utils', () => {
});
});
describe('formatDateAsMonth', () => {
it('should format dash cased date properly', () => {
const formattedMonth = datetimeUtility.formatDateAsMonth(new Date('2020-06-28'));
expect(formattedMonth).toBe('Jun');
});
it('should format return the non-abbreviated month', () => {
const formattedMonth = datetimeUtility.formatDateAsMonth(new Date('2020-07-28'), {
abbreviated: false,
});
expect(formattedMonth).toBe('July');
});
it('should format date with slashes properly', () => {
const formattedMonth = datetimeUtility.formatDateAsMonth(new Date('07/23/2016'));
expect(formattedMonth).toBe('Jul');
});
it('should format ISO date properly', () => {
const formattedMonth = datetimeUtility.formatDateAsMonth('2016-07-23T00:00:00.559Z');
expect(formattedMonth).toBe('Jul');
});
});
describe('formatDate', () => {
it('should format date properly', () => {
const formattedDate = datetimeUtility.formatDate(new Date('07/23/2016'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment