在React Native中使用时间格式化库format.js,可以通过安装相应的依赖包并引入使用。

首先,需要安装moment.js库和react-moment库:

npm install moment react-moment

然后,在需要使用时间格式化的组件中,引入moment和react-moment,并使用moment来处理时间格式化:

import React from 'react';
import moment from 'moment';
import Moment from 'react-moment';

const MyComponent = () => {
  // 格式化当前时间
  const formattedTime = moment().format('YYYY-MM-DD HH:mm:ss');

  return (
    <div>
      <p>当前时间:{formattedTime}</p>
      <p>使用react-moment组件格式化当前时间:<Moment format="YYYY-MM-DD HH:mm:ss" /></p>
    </div>
  );
};

export default MyComponent;

在上面的例子中,我们使用moment().format来格式化当前时间,并在组件中显示。另外,我们也可以使用react-moment组件来格式化时间,只需要在组件中使用标签,并设置format属性。

注意,如果你想使用其他语言的时间格式化,可以使用moment.js的本地化功能,具体请参考moment.js的文档。


原文地址: http://www.cveoy.top/t/topic/i5xb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录