Anik Anik - 1 month ago 9x
Javascript Question

Time string to date object conversion js angularJS

I am getting following string from REST api,
I want to make it 20/02/2016

I am using angularJS. So I will require a filter.
I have tried following

app.filter('myDateFilter', function() {

return function(input) {

var st = input;
var pattern = /(\d{4})(\d{2})(\d{2})/;
var date = new Date(st.replace(pattern, '$1-$2-$3'));

return date;



And in html I have used

{{t["due-date"] | myDateFilter}}

This returns 2016-02-20T00:00:00.000Z

Regular expression issue? Can you kindly give me proper code which should have been used instead to generate 20/02/2016.


Naively converting a Date into a string results in the output you are seeing:

console.log(new Date()) // "2016-09-02T15:19:07.921Z"

Instead, make sure you format the date into a string manually before returning it. E.g. toLocaleDateString() converts the Date into a string, taking into account the browser's locale:

console.log(new Date().toLocaleDateString()) // "09/02/2016"