Alessandro Celeghin
Alessandro Celeghin

Reputation: 4209

How do I extract only the day/month/year from a standard JavaScript date?

I am using angular 4, I have a date like this: 2017-09-02T01:44:13Z

This date needs to be binded to a calendar so I need to take only day-month-year from that date. Is there a simple way to get this? Maybe without getting day,month,year separately in a string and anfter converting to a date.

Upvotes: 1

Views: 131

Answers (1)

KIA
KIA

Reputation: 192

You can use the ECMAScript Intl Api to fromat the date and use in what ever format you want to use or make your own custom pipe to do whatever you want to do.

The basic code will be like this only

 var value = '2017-09-02T01:44:13Z';

//Optional 
//var options = { year: 'numeric', month: 'long', day: 'numeric' };

var newDate = new Intl.DateTimeFormat('en-GB').format(Date.parse(value));
document.getElementById("dateID").innerHTML=newDate;

Here is a working Plunkr which will help you.

If you want a custom pipe also then comment, I have made a custom angular pipe using the Intl API.

Hope this helps.

Upvotes: 1

Related Questions