我有一个代码可以将任何以秒为单位的日期转换为日日月日。那就是:将以秒为单位的日期转换为“日月年”日期
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脚本来使它工作。
重复? [将时间间隔以秒为单位转换为更易读的形式](https://stackoverflow.com/questions/8211744/convert-time-interval-given-in-seconds-into-more-human-readable-form) –
谢谢期待你的答复。我不认为它是重复的,因为我已经有我的代码来改变我的日期,我的问题是在我的AngularJS代码中实现它 – marcvander