【问题标题】:Moment js is not working with JSON in React NativeMoment js 不适用于 React Native 中的 JSON
【发布时间】:2020-03-05 16:49:45
【问题描述】:

这是我的 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>
    )    
}

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

【问题讨论】:

  • 试试"DD.MM.YYYY HH:mm:ss"

标签: javascript json reactjs react-native


【解决方案1】:

Moment 正在工作。但是您的时间字符串错误。您需要将日期格式字符串更改为 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'))
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"&gt;&lt;/script&gt;

【讨论】:

    【解决方案2】:

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

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

    检查解析docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 2020-05-27
      • 2018-07-20
      • 2021-08-16
      • 2016-11-04
      相关资源
      最近更新 更多