gpt4 book ai didi

ios - 有没有一种方法可以使用 React-Intl 正确格式化,而不会出现此错误?

转载 作者:行者123 更新时间:2023-11-29 05:46:20 25 4
gpt4 key购买 nike

我正在开发一个 React Native 应用程序,我需要 post_date我从第三方 API 返回的信息不显示为: 2019-05-10 11:26:39 ,而是显示:x many days ago .

我尝试安装 React-Intl,然后在 App.js 中实现它像这样:

import React from "react";
import { StyleSheet, Text, View } from "react-native";
import {
createBottomTabNavigator,
createStackNavigator,
createAppContainer
} from "react-navigation";
import { Provider } from "react-redux";
import { IntlProvider } from "react-intl";
import store from "./store";
import AuthScreen from "./screens/AuthScreen";
import WelcomeScreen from "./screens/WelcomeScreen";
import MapScreen from "./screens/MapScreen";
import DeckScreen from "./screens/DeckScreen";
import SettingsScreen from "./screens/SettingsScreen";
import ReviewScreen from "./screens/ReviewScreen";

const MainNavigator = createAppContainer(
createBottomTabNavigator({
welcome: WelcomeScreen,
auth: AuthScreen,
main: {
screen: createBottomTabNavigator({
map: MapScreen,
deck: DeckScreen,
review: {
screen: createStackNavigator({
review: ReviewScreen,
settings: SettingsScreen
})
}
})
}
})
);

export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<View style={styles.container}>
<IntlProvider locale="en">
<MainNavigator />
</IntlProvider>
</View>
</Provider>
);
}
}

在我的 DeckScreen.js 中像这样:

import React, { Component } from "react";
import { View, Text, Platform } from "react-native";
import { MapView } from "expo";
import { connect } from "react-redux";
import { Card, Button } from "react-native-elements";
// import Moment from "react-moment";
import { FormattedRelative } from "react-intl";
import Swipe from "../components/Swipe";
import * as actions from "../actions";

class DeckScreen extends Component {
renderCard(job) {
return (
<Card title={job.title}>
<View style={{ height: 300 }}>
<MapView
scrollEnabled={false}
style={{ flex: 1 }}
cacheEnabled={Platform.OS === "android" ? true : false}
/>
</View>
<View style={styles.detailWrapper}>
<Text>{job.company.id}</Text>
<FormattedRelative value={job.post_date.toString()} />
</View>
<Text>
{job.description.replace(/<span>/g, "").replace(/<\/span>/g, "")}
</Text>
</Card>
);
}

我觉得用 React-Intl 比用 Momentjs 更接近我想要的东西,但我收到了这个错误:

console.error: "[React Intl] Error formatting relative time. RangeError: The date value provided to IntlRelativeFormat#format() is not in valid range."

我的代码格式不正确吗?任何人都有如何实现的经验FormattedRelative来自 React-Intl?

我知道我可以使用toDateString()删除时间戳部分 2019-05-10 11:26:39但是当我尝试通过 <FormattedRelative value={job.post_date.toDateString()} /> 实现它时,我继续收到相同的错误消息。

最佳答案

您应该将日期格式转换为适当的毫秒并传递到 FormattedRelative 值属性中。

// convert 2019-05-10 11:26:39. date to millisecond, for example see below code.

var postDate = Date.now() - (1000 * 60 * 60 * 24);
<ul>
<li><FormattedRelative value={postDate} /></li>
</ul>

输出:

yesterday

关于ios - 有没有一种方法可以使用 React-Intl 正确格式化,而不会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56122306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com