0

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.

KENdi
  • 7,576
  • 2
  • 16
  • 31
marcvander
  • 479
  • 4
  • 17
  • Duplicate ? [Convert time interval given in seconds into more human readable form](https://stackoverflow.com/questions/8211744/convert-time-interval-given-in-seconds-into-more-human-readable-form) – Jeremy Thille Jul 25 '17 at 12:29
  • Thanks for your reply. I don't think it is a duplicate, since I already had my code to transform my date, my issue was to implement it in my AngularJS code – marcvander Jul 25 '17 at 13:19

4 Answers4

1

In fact you could use MomentJS and some Angular wrapper for it (for example, angular-moment on GitHub) in order to do any operation with dates. At least you will "skip" maintaining and debugging your code (keyword: timezones are problematic sometimes).

Alex N.
  • 56
  • 4
0

Since you have a function code for the functionality, just wrap that code into a function like this:

function ConvertDate(DateInSeconds) {
    var tripbegindateseconds = DateInSeconds;
    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;
    return tripbegindate;
}

and use it on those dates like this:

tripBeginDate: this.ConvertDate(trip.val().begindate),
tripEndDate: this.ConvertDate(trip.val().enddate)
Fahad Nisar
  • 1,723
  • 12
  • 18
  • Thanks, it worked with your answer! I am facing another problem with my AngularJS, maybe you can have a look : https://stackoverflow.com/questions/45305604/use-href-in-an-angularjs-ng-repeat-div – marcvander Jul 25 '17 at 14:01
0

You can Do

      var d=new Date(trip.val.begindate*1000) ;//time is in seconds,convert it to miliseconds
      d.getMonth();//this will return you month in integer(e.g-january=0 and so on)
      d.getFullYear();//this will return you year
      d.getDate();//this will return you date
      d.getDay();//this will return you day in integer like month
Debabrata
  • 488
  • 4
  • 13
0

I suggest you to use moment-https://momentjs.com/ where we can parse to any date formats as shown below:

moment(1454521239279).format("DD MMM YYYY hh:mm a") //parse integer

divyameher
  • 134
  • 8