【问题标题】:Adding date into Firebase将日期添加到 Firebase
【发布时间】:2015-12-31 00:18:46
【问题描述】:

有没有办法将日期选择器值添加到 Firebase 数据库中?

类似的东西?

  $scope.AddPost = function() {
          ...
          Published: $scope.post.Published.DATESTAMP
          ...
        }

来自materialize datepicker 值,如下所示:

<input type="date" ng-model="post.Published" class="datepicker">

来自选择器的服务器端代码:

<input type="text" ng-model="post.Published" class="datepicker ng-pristine ng-valid picker__input ng-touched picker__input--active picker__input--target" readonly="" id="P2078770150" tabindex="-1" aria-haspopup="true" aria-expanded="true" aria-readonly="false" aria-owns="P2078770150_root">

尝试过:

$scope.AddPost = function() {
    myPosts.$add({
      Title: $scope.post.Title,
      Intro: $scope.post.Intro,
      Body: $scope.post.Body,
      Published: $scope.post.Published,
      Author:$scope.post.Author
    })

加载除日期之外的所有内容。

我一直在寻找一个等价物,但只找到了 Firebase.ServerValue.TIMESTAMP,这不是我想要的,因为我需要人们能够手动选择日期。

【问题讨论】:

  • 这个问题有两个方面:从用户那里获取日期与在 Firebase 中存储数据。这两个是不同的主题,有很多方法可以混合它们。在 Firebase 中存储日期通常通过以下两种方式中的一种(或两种)完成:将其存储为时间戳与将其存储为字符串。你应该做哪一个,取决于你,什么最适合你的用例。
  • 如果您在从日期选择器中获取值时遇到问题,您必须展示您已经尝试过的内容。
  • 我在上面编辑了我的问题。我试图按原样输入它,但没有加载。有没有办法告诉 Firebase 它应该是哪种格式?

标签: firebase datestamp


【解决方案1】:

就像@Frank 所说,日期可以存储为字符串或firebase 中的时间戳,具体取决于您的要求。

试试

$scope.AddPost = function() {
      ...
      Published: new Date($scope.post.Published).getTime();
      ...
    }

【讨论】:

  • 啊!我把问题复杂化了。我尝试将两者都作为时间戳然后 toString() 似乎是一个更好的选择,但是它在 RFC 2822 中打印整个内容,即 2015 年 10 月 2 日 19:06:50 UTC 有没有办法将其缩减为仅日期?
  • 请先搜索再询问。例如。 JavaScript convert timestamp to string 应该给出很多相关的现有答案。
  • 是的,我已经尝试过:toString("dd/mm/yyyy") 并在我从数据中取回它时将它实际转换回我的模型中的角度,即 {{post.Published |日期:'mediumDate'}}来源,但仍然无济于事。这是“很多不同”答案的问题,至少有三种不同的方法可以解决这个问题。必须找到正确的语法/方法。
【解决方案2】:

我已经通过各种线程设法解决了这个问题。所以我

HTML

 <div class="form-group">
                        <label class="col-md-4 control-label" for="numDate">Choose a Date</label>
                        <input type="date" name="Published" ng-model="post.Published" class="datepicker" required>
                        <span class="error" ng-show="input.Published.$error.required">
                    </div>

在 AddPost() 函数中:

Published: new Date($scope.post.Published).getTime(),

角度

<p>By: {{post.Author}} &nbsp; Published on: {{post.Published |    date:'mediumDate'}}</p>

因此,我没有尝试在添加过程中进行转换,而是保留了一个时间戳,然后在视图中对其进行了转换。

谢谢

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2021-10-23
    • 2021-08-08
    • 2010-11-28
    相关资源
    最近更新 更多