AngularJS. Convert tag value (Unix time to human-readable time)

前端 未结 6 1140
萌比男神i
萌比男神i 2020-12-01 03:37

I am getting data from a database and displaying it:

    
  • {{item.date}}
相关标签:
6条回答
  • 2020-12-01 03:44

    Use format date filter like this:

    <mydate>{{item.date * 1000 | date:'yyyy-MM-dd HH:mm:ss Z'}}</mydate>
    

    Reference

    0 讨论(0)
  • 2020-12-01 03:45

    You should use the date filter that is already provided by angular: here

    0 讨论(0)
  • 2020-12-01 03:54

    I have faced the issue with unix time formatted as a number of seconds from the epoch start or as a number of milliseconds that is used in JavaScript. So strictly speaking, AngularJS doesn't convert Unix timestamp to Date, but a number with milliseconds, which is 1000 times larger, so first you will have to multiply your input number by 1000, like this:

    <mydate>{{item.date * 1000 | date:'yyyy-MM-dd HH:mm:ss Z'}}</mydate>
    

    Otherwise your date will be wrong.

    0 讨论(0)
  • 2020-12-01 03:56

    There is directive to that called rsTime.

        <rs-time start-time="1478513323" digital-format="'ffffd MMM d h:mm TT'"></rs-time>
    

    Check demo at Plunker .

    Below are different formates of time:

     M/d/y -  11/7/2016
    
     HH:mm:ss - 16:02:31 (24hrs formate)
    
     hh:mm:ss TT - 04:03:10 PM
    
     ffffd MMM d h:mm TT  - Mon Nov 7 at 4:04 PM
    

    For documentation visit Github

    0 讨论(0)
  • 2020-12-01 04:04

    If you have a Unix timestamp, you'll probably have to multiply your timestamp by 1000 since the Unix timestamp is in seconds and AngularJs date filter needs milliseconds.

    vm.milliseconds = Date('1441981121' * 1000);
    

    then use $filter() function

    var date = $filter('date')(vm.milliseconds, 'd MMMM yyyy');
    

    or you can use in ng-bind

    <span ng-bind="myController.milliseconds | date : 'd MMMM yyyy'"></span>
    
    0 讨论(0)
  • 2020-12-01 04:06
     yourapp.filter('timestampToDate', function () {
        return function (timestamp) {
            var date = new Date(timestamp * 1000);
            var dateObject = date.getFullYear() +'/'+ ('0' + (date.getMonth() + 1)).slice(-2) +'/'+ ('0' + date.getDate()).slice(-2);
            return dateObject;
        };
    });
    

    Usage:
    {{timestamp | timestampToDate}}

    0 讨论(0)
提交回复
热议问题