最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Parse string into Date in angularjs - Stack Overflow

programmeradmin2浏览0评论

I have data from the server to display. I receive strings such as this: 2016-05-01. I need to filter the data between two dates in that format. I have a function to parse the date as it is:

$scope.parseDate = function (date) {
  return new Date(Date.parse(date));
} 

In my html I display the date with this:

{{ parseDate(item.startDate) }}`

But in the browser, the result is: "2016-07-12T00:00:00.000Z".

  1. I need the parse the string into the same format as from the server: 2016-05-01.

  2. I need a simple filter function.

I don't want to use moment.js. My backend platform is JavaScript.

I have data from the server to display. I receive strings such as this: 2016-05-01. I need to filter the data between two dates in that format. I have a function to parse the date as it is:

$scope.parseDate = function (date) {
  return new Date(Date.parse(date));
} 

In my html I display the date with this:

{{ parseDate(item.startDate) }}`

But in the browser, the result is: "2016-07-12T00:00:00.000Z".

  1. I need the parse the string into the same format as from the server: 2016-05-01.

  2. I need a simple filter function.

I don't want to use moment.js. My backend platform is JavaScript.

Share Improve this question edited Sep 18, 2017 at 13:40 senderle 151k36 gold badges217 silver badges238 bronze badges asked Sep 18, 2017 at 12:44 KK SKKK SK 1011 gold badge2 silver badges11 bronze badges 3
  • Possible duplicate of Javascript change date into format of (dd/mm/yyyy) – TheCog19 Commented Sep 18, 2017 at 12:48
  • if you want to show the exact from the server. then display as it is. I cannot understand what you are trying to do. – Sibiraj Commented Sep 18, 2017 at 12:49
  • what is the format do you want to outpt? – firegloves Commented Sep 18, 2017 at 12:49
Add a comment  | 

5 Answers 5

Reset to default 9

Try this in your html :

{{item.startDate | date: 'MMM dd, yyyy'}}

You can use AngularJS filter like below-

$scope.parseDate = function (date) {
    $filter('date')(new Date(Date.parse(date)), 'yyyy-MM-dd');
} 

Something like

function(date) {
   var d = new Date(Date.parse(date));
   return d.getDate() + " " + d.getMonth() + " " + d.getFullYear();
}

Obviously it depends on what date format you desire to output.

Keep in mind that a function is not the best way to format a date in angular because it will be executed continuously and that cause a bad performance problem. You should use a filter like this

app.filter('mydate', function () {
    return function (input) {
        if (input) {

        var d = new Date(input);
        return d.getDate() + " " + d.getMonth() + " " + d.getFullYear();

    }
};
});

In your HTML file try using

{{ parseDate(item.startDate) | date:'yyyy-MM-dd'}}

You can use inbuilt date filter which Angular provides.

Something like this;

{{ date_variable | date:'dd-mm-yyyy' }}
发布评论

评论列表(0)

  1. 暂无评论