如何在用户提交每个新帖子时给它们加上时间戳(并将其保存在Firebase中)

How to timestamp each new post (and saved with it in Firebase) as users submit them?

本文关键字:时间戳 保存 Firebase 存在 提交 用户 新帖      更新时间:2023-09-26

我知道当前日期和时间来自new Date.getTime()

我如何实现它,以便所有用户的每一篇新帖子都会盖上日期和时间,并与帖子内容一起保存?

var timestamp = new Date.getTime()还是$scope.timestamp = new Date.getTime()?当使用ng-click=submitPost()提交表单时,如何在数组中保存日期?

我插入以下内容,用日期和时间标记每个条目。提交后控制器内:

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

在html 中

{{post.timestamp}}


更新条目的时间戳注释:

$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 = '';
    };

分配日期的最佳方式是使用toISOString

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

使用ISO格式,您可以避免所有特定于环境/浏览器的问题。