简体   繁体   中英

Moment js is not working with JSON in React Native

This is my 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.

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

It is showing. But if I am using Moment.js.I am getting Invalid date.

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

MM - month

mm - minutes

hh - 12 hrs

HH - 24 hrs

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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