0

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.

HGB
  • 2,157
  • 8
  • 43
  • 74
  • 1
    There are two sides to this question: getting a date from the users vs storing a data in Firebase. The two are separate topics and there are lots of way to mix them. Storing a date in Firebase is commonly done in one (or both) of two ways: store it as a timestamp vs store it as a string. Which one you should do, is up to you and what best fits your use-case. – Frank van Puffelen Oct 02 '15 at 18:00
  • If you're having problems getting the value from the datepicker, you will have to show what you've already tried. – Frank van Puffelen Oct 02 '15 at 18:01
  • I have edited my question above. I tried to enter it as it is but nothing loads. Is there a way to tell Firebase in which format it should be? – HGB Oct 02 '15 at 18:54

2 Answers2

1

Like @Frank said, date can be either stored as a string or as a timestamp in firebase depending upon your requirement.

try

$scope.AddPost = function() {
      ...
      Published: new Date($scope.post.Published).getTime();
      ...
    }
DavidW
  • 29,336
  • 6
  • 55
  • 86
  • Ah! I was over complicating the issue. I tried both as time stamp then toString() seems to be a better option, however it prints the whole thing i.e.Friday, 02-Oct-15 19:06:50 UTC in RFC 2822 Is there a way to trim it down to only the date? – HGB Oct 02 '15 at 19:08
  • Please search before asking. E.g. `JavaScript convert timestamp to string` should give *a lot* of relevant existing answers. – Frank van Puffelen Oct 02 '15 at 19:22
  • Yes, I have and tried: toString("dd/mm/yyyy") and by actually converting it back in angular in my model when I get it back from the data i.e. {{post.Published | date:'mediumDate'}}source but still no avail. This is the problem with 'lots of different' answers and three different ways at least to go about it. One has to find the right syntax/method. – HGB Oct 02 '15 at 19:39
1

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

HGB
  • 2,157
  • 8
  • 43
  • 74