Javascript 将以秒为单位的日期转换为';年月日';日期

Javascript 将以秒为单位的日期转换为';年月日';日期,javascript,angularjs,firebase,firebase-realtime-database,Javascript,Angularjs,Firebase,Firebase Realtime Database,我有一个代码,可以将任何以秒为单位的日期转换为日-月-年日期。这是: var database = firebase.database(); database.ref(`trips/${userid}/trips/${tripid}/begindate`).once('value').then(photosSnap => { var tripbegindateseconds = photosSnap.val(); var tripbegindatefull

我有一个代码,可以将任何以秒为单位的日期转换为日-月-年日期。这是:

    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脚本中以使其正常工作。

因为您有一个函数代码用于该功能,只需将该代码包装到如下函数中:

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;
}
在这样的日期使用它:

tripBeginDate: this.ConvertDate(trip.val().begindate),
tripEndDate: this.ConvertDate(trip.val().enddate)

事实上,您可以使用MomentJS和一些角度包装器(例如,GitHub上的Angular moment),以便对日期执行任何操作。至少您将“跳过”维护和调试代码(关键字:时区有时会有问题)。

您可以这样做

      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

我建议您使用矩-我们可以解析为任何日期格式,如下所示:


矩(1454521239279).format(“DD-MMM-yyy-hh:mma”)//解析整数

重复?谢谢你的回复。我不认为它是重复的,因为我已经有我的代码来转换我的日期,我的问题是在我的AngularJS代码中实现它谢谢,它与你的答案一起工作!我的AngularJS还有一个问题,也许你可以看看: