Skip to content

Commit

Permalink
Initial implementation of showing relative duration on date fields
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Feb 12, 2020
1 parent ee6aa46 commit 438cb77
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
EuiToolTip,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage, FormattedDate, FormattedTime, FormattedNumber } from '@kbn/i18n/react';
import {
FormattedMessage,
FormattedDate,
FormattedTime,
FormattedNumber,
FormattedRelative,
} from '@kbn/i18n/react';
import { useDispatch } from 'react-redux';
import styled from 'styled-components';
import { usePageId } from '../use_page_id';
Expand Down Expand Up @@ -49,12 +55,18 @@ const TruncateTooltipText = styled(TruncateText)`
`;

const FormattedDateAndTime: React.FC<{ date: Date }> = ({ date }) => {
return (
// If date is greater than or equal to 24h (ago), then show it as a date
// else, show it as relative to "now"
return Date.now() - date.getTime() >= 8.64e7 ? (
<>
<FormattedDate value={date} year="numeric" month="short" day="2-digit" />
{' @'}
<FormattedTime value={date} />
</>
) : (
<>
<FormattedRelative value={date} />
</>
);
};

Expand Down

0 comments on commit 438cb77

Please sign in to comment.