Search code examples
firebasedatestamp

Adding date into Firebase


Is there a way to add a date picker value into a Firebase database?

Something along these lines?

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

from a materialize datepicker value like so:

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

Server side code from picker:

<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">

TRIED:

$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
    })

Everything loads except date.

I have been looking for an equivalent but only found the Firebase.ServerValue.TIMESTAMP which is not what I want as I need people to be able to pick the date manually.


Solution

  • I have managed to fix this by going through various threads. So i

    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>
    

    IN THE AddPost() Function:

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

    ANGULAR

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

    So instead of trying to convert in the Add process, I kept is a Timestamp then converted it in the view.

    Thanks