2017-07-25 26 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変数は秒単位で日付を含みます。これらは私が1ヶ月の年の日付に変換しようとしている変数です。私はこの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 ")//解析する整数

関連する問題