mattermost-mobile/app/components/formatted_time.js
Elias Nahum 3b3a696a55 MM-18603 Fix post header to prevent overlaps (#3332)
* MM-18603 Fix post header to prevent overlaps

* Export BotTag and GuestTag
2019-09-27 17:16:36 -04:00

45 lines
1.2 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {Text} from 'react-native';
import moment from 'moment-timezone';
import CustomPropTypes from 'app/constants/custom_prop_types';
export default class FormattedTime extends React.PureComponent {
static propTypes = {
value: PropTypes.any.isRequired,
timeZone: PropTypes.string,
children: PropTypes.func,
hour12: PropTypes.bool,
style: CustomPropTypes.Style,
};
getFormattedTime = () => {
const {
value,
timeZone,
hour12,
} = this.props;
const format = hour12 ? 'hh:mm A' : 'HH:mm';
if (timeZone) {
return moment.tz(value, timeZone).format(format);
}
return moment(value).format(format);
};
render() {
const {children, style} = this.props;
const formattedTime = this.getFormattedTime();
if (typeof children === 'function') {
return children(formattedTime);
}
return <Text style={style}>{formattedTime}</Text>;
}
}