2017-07-25 186 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「)//解析整數

相關問題