mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
Add help functions for ISO8601 date format
This commit is contained in:
parent
601f6cbc5f
commit
6f7e6c2fc0
2 changed files with 49 additions and 1 deletions
|
|
@ -169,3 +169,28 @@ export const formatDate = (intl, todayString, d) => {
|
|||
|
||||
return `${formattedDate}, ${formattedTime}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts string given in ISO8601 format to date object.
|
||||
* This is used e.g. when when dates are parsed form urlParams
|
||||
*
|
||||
* @param {String} dateString in 'YYYY-MM-DD'format
|
||||
*
|
||||
* @returns {Date} parsed date object
|
||||
*/
|
||||
export const parseDateFromISO8601 = dateString => {
|
||||
return moment(dateString, 'YYYY-MM-DD').toDate();
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts date to string ISO8601 format ('YYYY-MM-DD').
|
||||
* This string is used e.g. in urlParam.
|
||||
*
|
||||
* @param {Date} date
|
||||
*
|
||||
* @returns {String} string in 'YYYY-MM-DD'format
|
||||
*/
|
||||
|
||||
export const stringifyDateToISO8601 = date => {
|
||||
return moment(date).format('YYYY-MM-DD');
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,13 @@
|
|||
import { fakeIntl } from './test-data';
|
||||
import { isDate, isSameDate, nightsBetween, daysBetween, formatDate } from './dates';
|
||||
import {
|
||||
isDate,
|
||||
isSameDate,
|
||||
nightsBetween,
|
||||
daysBetween,
|
||||
formatDate,
|
||||
parseDateFromISO8601,
|
||||
stringifyDateToISO8601,
|
||||
} from './dates';
|
||||
|
||||
describe('date utils', () => {
|
||||
describe('isDate()', () => {
|
||||
|
|
@ -87,4 +95,19 @@ describe('date utils', () => {
|
|||
expect(formatDate(fakeIntl, 'Today', d)).toEqual('2017-11-22, 13:51');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseDateFromISO8601()', () => {
|
||||
it('should return date', () => {
|
||||
const dateString = '2018-11-23';
|
||||
const date = new Date(2018, 10, 23);
|
||||
expect(parseDateFromISO8601(dateString)).toEqual(date);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stringifyDateToISO8601()', () => {
|
||||
it('should return string in YYYY-MM-DD format', () => {
|
||||
const date = new Date(2018, 10, 23);
|
||||
expect(stringifyDateToISO8601(date)).toEqual('2018-11-23');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue