将日期添加到Firebase中

将日期添加到Firebase中,firebase,datestamp,Firebase,Datestamp,有没有办法将日期选择器值添加到Firebase数据库中 类似这样的东西 $scope.AddPost = function() { ... Published: $scope.post.Published.DATESTAMP ... } 从如下所示的日期选择器值: <input type="date" ng-model="post.Published" class="datepicker"> 除日期外

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

类似这样的东西

  $scope.AddPost = function() {
          ...
          Published: $scope.post.Published.DATESTAMP
          ...
        }
从如下所示的日期选择器值:

<input type="date" ng-model="post.Published" class="datepicker">
除日期外,所有内容均已加载


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

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

试一试


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

HTML

棱角的

<p>By: {{post.Author}} &nbsp; Published on: {{post.Published |    date:'mediumDate'}}</p>
作者:{{post.Author}}发布日期:{{post.Published}日期:'mediumDate'}

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


谢谢

这个问题有两个方面:从用户那里获取日期和在Firebase中存储数据。这两个主题是分开的,有很多方法可以将它们混合在一起。在Firebase中存储日期通常有两种方式:将其存储为时间戳或将其存储为字符串。您应该做哪一个,取决于您以及什么最适合您的用例。如果您在从日期选择器获取值时遇到问题,您必须显示您已经尝试过的内容。我已编辑了上面的问题。我试着按原样进去,但什么也没装。有没有办法告诉Firebase它应该是哪种格式?啊!我把问题复杂化了。我尝试了两者作为时间戳,然后toString()似乎是一个更好的选择,但是它会在RFC 2822中打印整个内容,即周五,2015年10月2日19:06:50 UTC。是否有办法将其缩减为仅日期?请在询问之前先搜索。例如,
JavaScript将时间戳转换为字符串
应该会给出很多相关的现有答案。是的,我已经尝试过了:toString(“dd/mm/yyyy”),当我从数据(即{post.Published | date:'mediumDate}源代码)返回时,在我的模型中实际将其转换回角度,但仍然无效。这是一个有“许多不同”答案的问题,至少有三种不同的解决方法。必须找到正确的语法/方法。
$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
    })
$scope.AddPost = function() {
      ...
      Published: new Date($scope.post.Published).getTime();
      ...
    }
 <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>
Published: new Date($scope.post.Published).getTime(),
<p>By: {{post.Author}} &nbsp; Published on: {{post.Published |    date:'mediumDate'}}</p>