我有一個代碼可以將任何以秒爲單位的日期轉換爲日日月日。那就是:將以秒爲單位的日期轉換爲「日月年」日期
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