javascript - 将以秒为单位的日期转换为 'day month year' 日期

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

最佳答案

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

关于javascript - 将以秒为单位的日期转换为 'day month year' 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45303210/

相关文章:

javascript - 对两个变量使用相同的 if else 语句

javascript - HTML Mobile - 强制隐藏软键盘

regex - 如何在 Angular JS 范围内给出正则表达式模式

javascript - ng-hide完成后添加一个功能

android - 如何在Firebase聊天应用程序中实现消息可见功能

firebase - 如何修复flutter项目中的gradle错误

android - Firebase Crashlytics 无效的 api KEY AndroidX Gradle 3

javascript - 带有两个范围 slider 过滤器的 jQuery 数据表

javascript - 从 javascript : document. 突出显示 html 评估返回空节点

PHP switch case + PDO 更新数据库