将日期添加到 Firebase

Adding date into Firebase

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

类似的东西?

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

来自 materialize 日期选择器值,如下所示:

<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 这不是我想要的,因为我需要人们能够手动选择日期。

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

尝试

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

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

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(),

ANGULAR

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

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

谢谢