【问题标题】:Transforming date which is in seconds to a 'day month year' date将以秒为单位的日期转换为“日月年”日期
【发布时间】:2017-07-25 12:23:51
【问题描述】:

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

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

【问题讨论】:

标签: javascript angularjs firebase firebase-realtime-database


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    既然你有一个功能代码,只需将该代码包装成这样的函数:

    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)
    

    【讨论】:

    【解决方案3】:

    你可以做

          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
    

    【讨论】:

      【解决方案4】:

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

      moment(1454521239279).format("DD MMM YYYY hh:mm a") //解析整数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多