mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-27 19:42:11 +10:00
Add date/time format util
This commit is contained in:
parent
314ef549dd
commit
aa33ec8af6
3 changed files with 66 additions and 2 deletions
|
|
@ -22,3 +22,50 @@ export const nightsBetween = (startDate, endDate) => {
|
|||
}
|
||||
return nights;
|
||||
};
|
||||
|
||||
/**
|
||||
* Format the given date
|
||||
*
|
||||
* @param {Object} intl Intl object from react-intl
|
||||
* @param {String} todayString translation for the current day
|
||||
* @param {Date} d Date to be formatted
|
||||
*
|
||||
* @returns {String} formatted date
|
||||
*/
|
||||
export const formatDate = (intl, todayString, d) => {
|
||||
const paramsValid = intl && d instanceof Date && typeof todayString === 'string';
|
||||
if (!paramsValid) {
|
||||
throw new Error(`Invalid params for formatDate: (${intl}, ${todayString}, ${d})`);
|
||||
}
|
||||
const now = moment(intl.now());
|
||||
const formattedTime = intl.formatTime(d);
|
||||
let formattedDate;
|
||||
|
||||
if (now.isSame(d, 'day')) {
|
||||
// e.g. "Today, 9:10pm"
|
||||
formattedDate = todayString;
|
||||
} else if (now.isSame(d, 'week')) {
|
||||
// e.g. "Wed, 8:00pm"
|
||||
formattedDate = intl.formatDate(d, {
|
||||
weekday: 'short',
|
||||
});
|
||||
} else if (now.isSame(d, 'year')) {
|
||||
// e.g. "Aug 22, 7:40pm"
|
||||
formattedDate = intl.formatDate(d, {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
});
|
||||
} else {
|
||||
// e.g. "Jul 17 2016, 6:02pm"
|
||||
const date = intl.formatDate(d, {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
});
|
||||
const year = intl.formatDate(d, {
|
||||
year: 'numeric',
|
||||
});
|
||||
formattedDate = `${date} ${year}`;
|
||||
}
|
||||
|
||||
return `${formattedDate}, ${formattedTime}`;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { nightsBetween } from './dates';
|
||||
import { fakeIntl } from './test-data';
|
||||
import { nightsBetween, formatDate } from './dates';
|
||||
|
||||
describe('date utils', () => {
|
||||
describe('nightsBetween()', () => {
|
||||
|
|
@ -22,4 +23,20 @@ describe('date utils', () => {
|
|||
expect(nightsBetween(start, end)).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatDate()', () => {
|
||||
/*
|
||||
NOTE: These are not really testing the formatting properly since
|
||||
the fakeIntl object has to be used in the tests.
|
||||
*/
|
||||
|
||||
it('formats a date today', () => {
|
||||
const d = new Date(Date.UTC(2017, 10, 23, 13, 51));
|
||||
expect(formatDate(fakeIntl, 'Today', d)).toEqual('Today, 13:51');
|
||||
});
|
||||
it('formats a date', () => {
|
||||
const d = new Date(Date.UTC(2017, 10, 22, 13, 51));
|
||||
expect(formatDate(fakeIntl, 'Today', d)).toEqual('2017-11-22, 13:51');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -154,7 +154,7 @@ export const fakeIntl = {
|
|||
formatPlural: d => d,
|
||||
formatRelative: d => d,
|
||||
formatTime: d => `${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}`,
|
||||
now: d => d,
|
||||
now: () => Date.UTC(2017, 10, 23, 12, 59),
|
||||
};
|
||||
|
||||
const noop = () => null;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue