简体   繁体   English

如何将此日期转换为其他格式

[英]How to convert this date into a different format

Here is my Angular app 这是我的Angular应用

var filterData = angular.module('myApp',[]).controller('CallWebApi', function($scope, $http) {
    // Local version of the data
    $http.get('./events.js').
        success(function (data) {
            $scope.data = data.result.items;
            console.log('success ' + data)
        })
        .error(function(data) {
            console.log('failure ' + data)
        });
});
filterData.filter('removeSpacesThenLowercase', function () {
        return function (text) {
            var str = text.replace(/\s+/g, '-');
            return str.toLowerCase();
        };
})

The date looks like this in my data: 日期在我的数据中看起来像这样:

        "{B588A80F-A8C0-4A97-A35A-07D81ED53E9B}": {
            "Name": "Expiration Date",
            "Type": "Date",
            "Value": "20150827T000000"
        },

And here is my HTML: 这是我的HTML:

<table>

    <tr>
        <th>Name</th>
        <th>Location</th>
        <th>Date</th>
    </tr>
    <tr ng-repeat="item in data">
        <td><a href="{{ item.Name | removeSpacesThenLowercase }}">{{ item.Fields["{BB2389F3-555B-4FC6-B106-C0A23A55A15F}"].Value }}</a></td>
        <td>{{ item.Fields["{123A77C7-07D5-4CAA-85E0-8F9B9CEE110C}"].Value }}</td>
        <td>{{ item.Fields["{B588A80F-A8C0-4A97-A35A-07D81ED53E9B}"].Value }}</td>
    </tr>

</table>

How do I convert this date: 如何转换此日期:

20150827T000000 20150827T000000

Into this format: 转换成这种格式:

08/27/2015? 2015年8月27日?

This should work: 这应该工作:

var d = '20150827T000000';
console.log(d.substr(4,2) + '/' + d.substr(6,2) + '/' + d.substr(0,4));
// will give you '08/27/2015'

I created a demo on how you can integrate this in angular: http://plnkr.co/edit/KheLKhLHgGYoF0R5CZgf?p=preview 我创建了一个关于如何将其集成到angular中的演示: http : //plnkr.co/edit/KheLKhLHgGYoF0R5CZgf?p=preview

You can create a filter: 您可以创建一个过滤器:

myModule.filter('formatData', function () {
    return function (d) {
        return d.substr(4,2) + '/' + d.substr(6,2) + '/' + d.substr(0,4);
    };
})

and then use it in your view: 然后在您的视图中使用它:

 {{test["{B588A80F-A8C0-4A97-A35A-07D81ED53E9B}"].Value|formatData}}

and a fork that formats the data: http://plnkr.co/edit/EReUj4GkhGF36SS6RaX4?p=preview 以及用于格式化数据的fork: http : //plnkr.co/edit/EReUj4GkhGF36SS6RaX4?p=preview

you could try DateX an eXtension on built-in Date object (ps author) 您可以在内置的Date对象上尝试使用DateX和eXtension(ps作者)

example: 例:

var date = new DateX( );
var formatted = date.format('Y-m-d H:i:s');
var parsed = DateX.parse(formatted, 'Y-m-d H:i:s');
console.log(formatted);
console.log(parsed.format('Y-m-d H:i:s'));

for your example one can parse it (and validatre it at the same time) into an actual date 以您的示例为例,可以将其解析(并同时验证)为实际日期

var date = DateX.parse("20150827T000000", 'YmdHis');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM