开发者社区> 问答> 正文

#React 如何使用React Intl格式化日期?

#React 如何使用React Intl格式化日期?

展开
收起
因为相信,所以看见。 2020-05-07 20:31:18 820 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    在injectIntl()高次成分会给你访问该formatDate()方法通过在组件的道具。该方法在内部由的实例使用FormattedDate,它返回格式化日期的字符串表示形式

    import { injectIntl, intlShape } from 'react-intl'
    
    const stringDate = this.props.intl.formatDate(date, {
      year: 'numeric',
      month: 'numeric',
      day: 'numeric'
    })
    
    const MyComponent = ({intl}) => (
      <div>{`The formatted date is ${stringDate}`}</div>
    )
    
    MyComponent.propTypes = {
      intl: intlShape.isRequired
    }
    
    export default injectIntl(MyComponent)
    
    2020-05-07 20:31:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
利用编译将 Vue 组件转成 React 组件 立即下载
React Native 全量化实践 立即下载
React在大型后台管理项目中的工程实践 立即下载