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 moment from 'moment';
import PimItemHeader from './PimItemHeader';
import * as ICAL from 'ical.js';
import { formatDateRange } from '../helpers';
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 {
props: {
item?: EventType,

@ -1,4 +1,6 @@
import * as React from 'react';
import * as ICAL from 'ical.js';
import moment from 'moment';
// Generic handling of input changes
export function handleInputChange(self: React.Component, part?: string) {
@ -47,3 +49,35 @@ export function insertSorted<T>(array: T[] = [], newItem: T, key: string) {
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