简体   繁体   English

如何在React Native中制作时间格式

[英]How to make time format in react native

I have to display this expiryDate on two different screens , this value is coming from api . 我必须在两个不同的屏幕上显示此expiryDate,此值来自api。 Number 1 expiryDate is working coming in formate , but no 2 is not working , getting error invalid time I am getting error invalid time , please suggest . 第1个expiryDate正在起作用,但是第2个不起作用,正在得到错误无效时间我正在得到错误无效时间,请提出建议。

// 1.Below code is displaying proper format // 1.下面的代码显示正确的格式

expiryDate:
date: "2019-04-02T15:17:16.016Z"
__typename: "Date"

// 2 . // 2。 In below format i am getting error invalid time . 在以下格式中,我得到了错误无效时间。

expiryDate:
date: "2099-12-30T23:00:00.000Z"
__typename: "Date"

// Below code is using to format my expiryDate and after that i am displaying expDate //下面的代码用于格式化我的expiryDate,然后显示expDate

let expDate = new Date(expiryDate).toISOString().split('T')[0].split('-').reverse().join('-') 
              + ' ' 
              + new Date(expiryTime).toISOString().split('T')[1].split('.')[0];

I have to display in this formate " 2019-04-02 15:17:16 " 我必须在这个队友中展示“ 2019-04-02 15:17:16”

The beauty of the React Native is that it supports lots of JS Librarieis like Moment.js. React Native的优点在于它支持许多JS Librarieis,例如Moment.js。 Using moment.js would be a better/easier way to handle date/time instead coding from scratch 使用moment.js将是处理日期/时间的更好/更简便的方法,而不是从头开始编码

just run this in the terminal: 只需在终端中运行此命令:

npm install moment --save

And in your React Native js page: 在您的React Native js页面中:

import Moment from 'moment'; 从“时刻”导入时刻;

render(){
    Moment.locale('en');
    var dt = '2016-05-02T00:00:00';
    return(<View> {Moment(dt).format('d MMM')} </View>) //basically you can do all sorts of the formatting and others
}

You may check the moment.js offical docs at here https://momentjs.com/docs/ 您可以在这里https://momentjs.com/docs/查看一下moment.js官方文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM