【发布时间】:2019-10-22 11:42:43
【问题描述】:
我在我的 React 应用程序中使用 moment JS 来格式化日期,这不是使用 create-react-app 设置的。我想将时间戳格式化为如下可读格式:
- 如果提供的时间戳是今天的,它应该只显示时间。例如:
2:00 pm。 - 如果提供的时间戳是昨天的,它应该显示昨天没有时间。例如:
Yesterday。 - 如果提供的时间戳是昨天之前的任何时间,它应该显示
没有时间的确切日期。例如:
24 Feb, 2019。
到目前为止我的代码: webpack.config.js:-
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
....
plugins: [
new MomentLocalesPlugin({
localesToKeep: ['es-us', 'hi']
})
]
App.js
import React, { useEffect } from 'react';
import moment from 'moment';
const App = () => {
const timestamp = '1571744305';
return (
<Router>
<div>
{moment(timestamp).calendar()}//08/21/2019
</div>
);
};
export default App;
【问题讨论】:
-
请使用humanize npmjs.com/package/humanize。
标签: javascript reactjs webpack momentjs