Move event date formatting function to a shared place.

master
Tom Hacohen 6 years ago
parent 4db3b24743
commit c2d4027e13

@ -1,44 +1,11 @@
import * as React from 'react'; import * as React from 'react';
import moment from 'moment';
import PimItemHeader from './PimItemHeader'; import PimItemHeader from './PimItemHeader';
import * as ICAL from 'ical.js'; import { formatDateRange } from '../helpers';
import { EventType } from '../pim-types'; import { EventType } from '../pim-types';
function formatDateRange(start: ICAL.Time, end: ICAL.Time) {
const mStart = moment(start.toJSDate());
const mEnd = moment(end.toJSDate());
let strStart;
let strEnd;
const allDayFormat = 'dddd, LL';
const fullFormat = 'LLLL';
// All day
if (start.isDate) {
if (mEnd.diff(mStart, 'days', true) === 1) {
return mStart.format(allDayFormat);
} else {
strStart = mStart.format(allDayFormat);
strEnd = mEnd.clone().subtract(1, 'day').format(allDayFormat);
}
} else if (mStart.isSame(mEnd, 'day')) {
strStart = mStart.format(fullFormat);
strEnd = mEnd.format('LT');
if (mStart.isSame(mEnd)) {
return strStart;
}
} else {
strStart = mStart.format(fullFormat);
strEnd = mEnd.format(fullFormat);
}
return strStart + ' - ' + strEnd;
}
class Event extends React.PureComponent { class Event extends React.PureComponent {
props: { props: {
item?: EventType, item?: EventType,

@ -1,4 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import * as ICAL from 'ical.js';
import moment from 'moment';
// Generic handling of input changes // Generic handling of input changes
export function handleInputChange(self: React.Component, part?: string) { export function handleInputChange(self: React.Component, part?: string) {
@ -47,3 +49,35 @@ export function insertSorted<T>(array: T[] = [], newItem: T, key: string) {
return array; return array;
} }
export function formatDateRange(start: ICAL.Time, end: ICAL.Time) {
const mStart = moment(start.toJSDate());
const mEnd = moment(end.toJSDate());
let strStart;
let strEnd;
const allDayFormat = 'dddd, LL';
const fullFormat = 'LLLL';
// All day
if (start.isDate) {
if (mEnd.diff(mStart, 'days', true) === 1) {
return mStart.format(allDayFormat);
} else {
strStart = mStart.format(allDayFormat);
strEnd = mEnd.clone().subtract(1, 'day').format(allDayFormat);
}
} else if (mStart.isSame(mEnd, 'day')) {
strStart = mStart.format(fullFormat);
strEnd = mEnd.format('LT');
if (mStart.isSame(mEnd)) {
return strStart;
}
} else {
strStart = mStart.format(fullFormat);
strEnd = mEnd.format(fullFormat);
}
return strStart + ' - ' + strEnd;
}

Loading…
Cancel
Save