I have a code that works to transform any date which is in seconds to a day month year date. Here it is:
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);
});
I am now trying to implement this into an AngularJS code. I am retrieving data from a Firebase database, and displaying them with AngularJS. Here is my JS code to retrieve the data:
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));
});
Here my tripBeginDate and tripEndDate variables contain dates in seconds. These are the variables I'm trying to transform into a date with day month year. I don't know how to implement my code into this JS script to make it work.