Format Date time in AngularJS

后端 未结 13 1755
Happy的楠姐
Happy的楠姐 2020-11-30 22:00

How do I properly display the date and time in AngularJS?

The output below shows both the input and the output, with and without the AngularJS date filter:



        
13条回答
  •  萌比男神i
    2020-11-30 22:41

    I know this is an old item, but thought I'd throw in another option to consider.

    As the original string doesn't include the "T" demarker, the default implementation in Angular doesn't recognize it as a date. You can force it using new Date, but that is a bit of a pain on an array. Since you can pipe filters together, you might be able to use a filter to convert your input to a date and then apply the date: filter on the converted date. Create a new custom filter as follows:

    app
    .filter("asDate", function () {
        return function (input) {
            return new Date(input);
        }
    });
    

    Then in your markup, you can pipe the filters together:

    {{item.myDateTimeString | asDate | date:'shortDate'}}
    

提交回复
热议问题