簡體   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