将日期输入值存储在Firebase上 [英] Storing Date input values on Firebase

查看:142
本文介绍了将日期输入值存储在Firebase上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这就是我的代码看起来像
$ b $ pre $ cropc.child(mycrop.name).push({
cropname:mycrop.name,
croplocation:mycrop.location,
cropplantdate:mycrop.plantdate.toString(),
cropharvestdate:mycrop.harvestdate.toString(),
} )

mycrop.harvestdate mycrop.plantdate 都是来自我的html的日期输入

 < input type =日期ng-model ='mycrop.harvestdate'> 

< input type =dateng-model ='mycrop.harvestdate'>

为了能够将数据放在我的Firebase数据库中,我需要先将它转换为字符串

  cropplantdate:mycrop.plantdate.toString(),

但我的Firebase数据库中的数据包括时间和时区

示例数据

 星期六Dec 12 2020 00:00:00 GMT + 0800(马来半岛标准时间)

所以一旦我从我的数据库调用数据,我不能过滤它,因为它不是一个日期,而是一个字符串。
如何解决这个问题,这样我就可以过滤日期(这是转换为字符串)存储在我的数据库里面

解决方案

两个选项

1)将日期以更通用但人们可读的格式存储,所以现在将是11月27日星期日09:13:38 p>

  20161127091338 



<使用

 (new Date).getTime()/ 1000将日期存储为unix时间戳(以毫秒为单位) 

#2有许多变种,所以做一些研究,看哪个最适合您的使用case。

您可以将答案保存为一个字符串,但#1更容易搜索,因为查询不需要任何类型的转换 - 查看今天的事件< >

  queryStartingAt(20161127)and queryEndingAt(20161127)


So this is how my code looks like

cropref.child(mycrop.name).push({
                          cropname:mycrop.name,
                          croplocation:mycrop.location,
                          cropplantdate:mycrop.plantdate.toString(),
                          cropharvestdate:mycrop.harvestdate.toString(),         
                  })

mycrop.harvestdate and mycrop.plantdate are both date inputs from my html

<input type="date" ng-model='mycrop.harvestdate'>

<input type="date" ng-model='mycrop.harvestdate'>

to be able to put the data on my Firebase database , I need to convert it first into string

cropplantdate:mycrop.plantdate.toString(),      

but the data on my Firebase database includes time and timezone

sample data

Sat Dec 12 2020 00:00:00 GMT+0800 (Malay Peninsula Standard Time)

so once I call the data from my database, I can't filter it since it's not a date anymore but a string. How do I solve this problem so that I can filter date (which is converted to string) stored inside my database

解决方案

Two options

1) Store the date in a more generic, but human readable format, so right now would be Sunday November 27 at 09:13:38

20161127091338

2) Store the date as a unix timestamp (in milliseconds) using

(new Date).getTime() / 1000

There are a lot of variants to #2 so do some research to see which is best for your use case.

You can save either answer as a string but #1 would be more easily searchable since queries wouldn't require any kind of conversions - to see todays events

queryStartingAt("20161127") and queryEndingAt("20161127")

这篇关于将日期输入值存储在Firebase上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆