【问题标题】:React Native: How to save date in realm DBReact Native:如何在领域数据库中保存日期
【发布时间】:2017-12-17 13:04:13
【问题描述】:

我的领域模型是:

const itemsSchema = {
    name: 'items',
    properties: {
        key: 'int',
        business_id: 'int',
        item_id: 'int',
        item_name: 'string',
        item_price: 'float',
        datetime: 'date'
        // datetime: 'string'
    }
};

我想在每次用户点击保存按钮时保存当前日期,这是在保存时执行的代码,如果我将模型属性设为datetime: 'string',则会保存数据

realm.write(() => {
    realm.create('items', {
        key: 0,
        business_id: QRData.business_id,
        item_id: QRData.item_id,
        item_name: QRData.item_name,
        item_price: QRData.item_price,
        datetime: new Date('2017-06-06 21:23:53')
    });
});

但如果尝试将模型属性设置为 date 来保存它,则会引发此错误:

错误:值“无效日期”不能转换为数字。

【问题讨论】:

  • 2017-06-06 21:23:53 的日期格式似乎无效。
  • @LajosArpad 什么是有效格式?
  • 取决于您的服务器配置。

标签: javascript react-native realm


【解决方案1】:

代替

new Date('2017-06-06 21:23:53')

你可以用参数实例化日期,比如

new Date(2017, 6, 6, 21, 23, 53)

请注意,月份从 0 开始。

【讨论】:

  • 我应该把参数用引号括起来吗?
  • @Omer 你不需要。只需测试此实例化,如果可行,请接受我的回答。如果没有,请给我有关您看到的问题的信息。
【解决方案2】:

new Date('2017-06-06 21:23:53') 是无效格式。

试试new Date('2017-06-06T21:23:53')

它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    相关资源
    最近更新 更多