Это дает мне вот так, вместо того, чтобы показывать, сколько часов показывается "в часах", но реальная проблема в том, что, хотя дата указана сегодня, а время должно показывать в минутах, что она дает мне часы ..
Я должен отображать время, исходя из условия, что если заказ был размещен на несколько минут раньше, он должен показывать "1 min"
, если несколько часов назад, то "18 hr"
, если он не соответствует текущей дате, он должен показывать дату в формате * 1006. *. используя moment.js
любая помощь приветствуется.
Это мой код:
класс экспорта Уведомления расширяет PureComponent {
constructor(props) {
super(props);
this.state = {
notificationsData: null,
isLoading: true
};
}
componentDidMount() {
if (notifications) {
this.setState({
notificationsData: notifications.data,
isLoading: false
});
} else {
this.setState({ isLoading: false });
}
}
renderIconBadge(icon) {
return (
<View style={styles.iconBagdeContainer}>
<Icon name={icon} size={scale(16)} />
</View>
);
}
getDateFormatted(dateTime) {
var date = moment(dateTime, "DD/MM/YYYY HH:mm").isValid() ?
moment(dateTime, "DD/MM/YYYY HH:mm")
: moment(new Date(dateTime), "DD/MM/YYYY HH:mm");
var diff = date.fromNow();
if (diff.indexOf("minute") > -1) {
return diff.substring(0, 2).trim() + " mins";
} else if (diff.indexOf("hour") > -1) {
//If it's yesterday
if(date.day() != moment().day()){
return date.format("DD/MM/YYYY");
}
return diff.substring(0, 2).trim() + " hrs";
} else {
return date.format("DD/MM/YYYY");
}
}
renderNotificationCard = ({ item }) => {
return (
<Card style={styles.notificationCardContainer}>
<View
style={{
flexDirection: "row"
}}
>
{/* <View style={{ flexDirection: "row" }}> */}
{this.renderIconBadge(item.icon)}
<View
style={{
marginLeft: scale(12)
}}
>
<Text
style={styles.location}
numberOfLines={3}
ellipsizeMode="tail"
>
{item.location}
</Text>
<View style={{ flexDirection: "row" }}>
<ClickView onPress={() => {}}>
<Text style={styles.viewBooking}>
View Booking
</Text>
</ClickView>
<Text style={styles.dateFormat}>
{this.getDateFormatted(item.dateTime)}
</Text>
</View>
</View>
{/* </View> */}
</View>
</Card>
);
};
renderNotificationList() {
const { notificationsData } = this.state;
return (
<View style={{ marginTop: scale(10), marginBottom: scale(100) }}>
{notificationsData.notification.length !== 0 ? (
<FlatList
data={this.state.notificationsData.notification}
renderItem={this.renderNotificationCard}
/>
) : (
<View
style={{
marginTop: scale(100),
alignItems: "center"
}}
>
<Image source={Images.noTaskImg} />
<Text
style={{
marginTop: scale(20),
fontSize: scale(18),
fontWeight: "600",
color: Colors.body
}}
>
No Notifications found.
</Text>
</View>
)}
</View>
);
}
render() {
const { isLoading } = this.state;
return (
<ThemeView
theme="dark"
style={styles.mainContainer}
rightButton="close"
onRightButtonPress={() => {
this.props.navigation.goBack();
}}
>
{isLoading ? (
<ProgressBar />
) : (
<View>
<View
style={{ marginTop: scale(Metrics.headerHeight) }}
>
<Text style={styles.newTasks}>New Tasks</Text>
{this.renderNotificationList()}
</View>
</View>
)}
</ThemeView>
);
}
}
Формат данных JSON такой
{
id: "1",
icon: "water",
location:
"Your booking water",
dateTime: "2018-06-12T20:20:52.123Z"
}
Заранее спасибо