简体   繁体   English

Moment js 不适用于 React Native 中的 JSON

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

This is my JSON:这是我的 JSON:

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

I am getting date_time and outputing it on React Native app like this.我正在获取 date_time 并将其输出到这样的 React Native 应用程序上。

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

It is showing.它正在显示。 But if I am using Moment.js.I am getting Invalid date.但如果我使用 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>
    )    
}

Can anybody tell why I am getting an invalid date?谁能告诉我为什么我的日期无效?

Moment was working.But your time string was wrong.You need to change the date format string to YYYY-MM-DD or else mention the format on moment initiate时刻正在工作。但是您的时间字符串错误。您需要将日期格式字符串更改为YYYY-MM-DD或在启动时提及格式

MM - month MM - 月

mm - minutes mm - 分钟

hh - 12 hrs hh - 12 小时

HH - 24 hrs 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>

Your parsing format is incorrect.您的解析格式不正确。 Change to:改成:

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

Check the parsing docs检查解析文档

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

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