繁体   English   中英

Moment js 不适用于 React Native 中的 JSON

[英]Moment js is not working with JSON in React Native

这是我的 JSON:

orders:[{
   "buyerEmail": "unknow@gmail.com",
   "profile_name_ru": "ПРЕДПРИНИМАТЬ",
   "bin": "20940010947",
   "date_time": "15.10.2019 10:39:27"
   }
 ]

我正在获取 date_time 并将其输出到这样的 React Native 应用程序上。

<View>
 <Text>{orders.dateTime}</Text>
</View>

它正在显示。 但如果我使用 Moment.js。我会得到无效的日期。

render(){   
    const ed = (order.dateTime);
    const check = moment(new Date(ed)).format("DD.MM.YYYY, HH:MM");
   return (
        <View>
           <Text>{check}</Text>
         </View>
    )    
}

谁能告诉我为什么我的日期无效?

时刻正在工作。但是您的时间字符串错误。您需要将日期格式字符串更改为YYYY-MM-DD或在启动时提及格式

MM - 月

mm - 分钟

hh - 12 小时

HH - 24 小时

 let str = '15.10.2019 10:39:27'; console.log(new Date(str)) //check this its return null //so need to mention format while initialize console.log(moment(str,'DD.MM.YYYY HH:mm:ss').format('DD.MM.YYYY, hh:mm A'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

您的解析格式不正确。 改成:

moment("15.10.2019 10:39:27", "DD.MM.YYYY h:mm:ss");

检查解析文档

暂无
暂无

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

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