2017-07-25 187 views
0

我有一个代码可以将任何以秒为单位的日期转换为日日月日。那就是:将以秒为单位的日期转换为“日月年”日期

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脚本来使它工作。

+0

重复? [将时间间隔以秒为单位转换为更易读的形式](https://stackoverflow.com/questions/8211744/convert-time-interval-given-in-seconds-into-more-human-readable-form) –

+0

谢谢期待你的答复。我不认为它是重复的,因为我已经有我的代码来改变我的日期,我的问题是在我的AngularJS代码中实现它 – marcvander

回答

0

既然你有功能的功能代码,只是换行代码到这样的功能:

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) 
+0

谢谢,它与您的答案一起工作!我面对AngularJS的另一个问题,也许你可以看看:https://stackoverflow.com/questions/45305604/use-href-in-an-angularjs-ng-repeat-div – marcvander

1

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

0

你可以做

 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 
0

我建议你使用moment- https://momentjs.com/,我们可以解析到任何日期格式,如下图所示:

时刻(1454521239279).format(“DD MMM YYYY HH :mm a“)//解析整数

相关问题