【发布时间】:2017-07-25 12:23:51
【问题描述】:
我有一个代码可以将任何以秒为单位的日期转换为日月年日期。这里是:
var database = firebase.database();
database.ref(`trips/${userid}/trips/${tripid}/begindate`).once('value').then(photosSnap => {
var tripbegindateseconds = photosSnap.val();
var tripbegindatefull = new Date(0); // The 0 there is the key, which sets the date to the epoch
tripbegindatefull.setUTCSeconds(tripbegindateseconds);
var tripbeginmonth = tripbegindatefull.getUTCMonth() + 1; //months from 1-12
var tripbeginday = tripbegindatefull.getUTCDate();
var tripbeginyear = tripbegindatefull.getUTCFullYear();
tripbegindate = tripbeginday + "/" + tripbeginmonth + "/" + tripbeginyear;
$('#tripbegindate').html(tripbegindate);
});
我现在正在尝试将其实现到 AngularJS 代码中。我正在从 Firebase 数据库中检索数据,并使用 AngularJS 显示它们。这是我检索数据的 JS 代码:
var app = angular.module('test', []);
app.controller('MainCtrl', function($scope) {
var database = firebase.database();
database.ref(`trips/${userid}/trips`).once('value')
.then(photosSnap => {
var trips = [];
photosSnap.forEach((trip) => {
trips.push({
tripKey: trip.key,
tripName: trip.val().name,
tripPhotoUrl: trip.val().photourl,
tripBeginDate: trip.val().begindate,
tripEndDate: trip.val().enddate
});
});
$scope.repeatData = trips;
// apply immediatly, otherwise doesn't see the changes
$scope.$apply();
// returns the array in the console to check values
console.log($scope);
}).catch(err => alert(err));
});
这里我的 tripBeginDate 和 tripEndDate 变量包含以秒为单位的日期。这些是我试图将日期转换为日月年的变量。我不知道如何将我的代码实现到这个 JS 脚本中以使其工作。
【问题讨论】:
-
感谢您的回复。我不认为这是重复的,因为我已经有了转换日期的代码,我的问题是在我的 AngularJS 代码中实现它
标签: javascript angularjs firebase firebase-realtime-database