1

I understand that current date and time is from new Date.getTime()

How can I implement it so that each new post by all users will be stamped with a date and time and saved together with the post content?

Is it var timestamp = new Date.getTime() or $scope.timestamp = new Date.getTime()? How do I save the date in the array when form is submitted with ng-click=submitPost()?

I inserted the following to mark each entry with a date and time. Within the submit post controller:

$scope.post = {url: 'http://', title: '', timestamp: new Date()};

And in the html

{{post.timestamp}}


Update for timestamping comments to the entry:

$scope.addComment = function () {
        var comment = {
            text: $scope.commentText,
            creator: $scope.user.profile.username,
            creatorUID: $scope.user.uid,
            timestamp: new Date().toUTCString() //I added it here but doesnt work
        };
        $scope.comments.$add(comment);
        $scope.commentText = '';
    };
Thinkerer
  • 1,606
  • 6
  • 23
  • 43
  • can you add a jsfiddle/plunkr that demonstrates the problem? – Yaron Schwimmer Oct 25 '14 at 11:45
  • Yarons here it is. http://plnkr.co/edit/sXZyfSufQYe9nElrvPEf the problem is the favtory is different from the plumkr example where its a static array. Plunkr works but actual doesnt work. – Thinkerer Oct 25 '14 at 11:55
  • your question is not very clear, can you please restate it... – harishr Oct 25 '14 at 16:17
  • @HarishR basically, everytime a user post a message, i hope to have a date and time the post is written and submitted (saved in firebase with the post text entry and title) – Thinkerer Oct 26 '14 at 06:55

1 Answers1

0

the best way to assign a date is to use toISOString

 $scope.post = {url: 'http://', title: '', timestamp: new Date().toISOString()};

using ISO format you can avoid all the environment/browser specific issues.

harishr
  • 17,807
  • 9
  • 78
  • 125
  • Thanks @HarishR. What if its for each comments to the post itself? I have updated the comment codes above. – Thinkerer Oct 26 '14 at 15:38
  • you can use exact same way as above for comments as well – harishr Oct 26 '14 at 15:39
  • In the code above, unlike post, comments dont have an array. How do I then insert the relevant timestamp definitions in the code? Thanks! – Thinkerer Oct 26 '14 at 15:41
  • property is always added to object, never to array. In array you push objects. so you have to specify timestamp property to object. when you say it doesnt work, can you always be to the point what does not work. just saying: `it doesnt work` does not help at all – harishr Oct 26 '14 at 15:44
  • My apologies. It is object with {}. Doesnt work meaning the timestamp does not appear, and appears as {{comment.timestamp}}, which is whats written in the html. This means the expression is not recognised. You are right that I want to have the timestamp property to the object itself, here being the comment. – Thinkerer Oct 26 '14 at 15:48
  • can you please update your plunker with what is not working, will take a look at the plunker. – harishr Oct 26 '14 at 15:50
  • Im not sure how to set up Firebase with Plunkr and all. Attached is a modification of the existing Plunkr. The idea is that date and time should be auto appended to each comment added to a post. http://embed.plnkr.co/QKGMIZWVwjHPrPXATY3U/ – Thinkerer Oct 27 '14 at 16:00
  • I see no reason for it to not work, logically it is similar to Posts. so if its not working its surely something not relation to angular. – harishr Oct 27 '14 at 16:03