I'm receiving date and time from the REST API in the below format
2016-01-17T:08:44:29+0100
I want to format this date and time stamp like
17-01-2016 08:44:29
It should be dd/mm/yyyy hh:mm:ss
How to format this in TypeScript?
I'm receiving date and time from the REST API in the below format
2016-01-17T:08:44:29+0100
I want to format this date and time stamp like
17-01-2016 08:44:29
It should be dd/mm/yyyy hh:mm:ss
How to format this in TypeScript?
you can use moment.js. install moment js in your project
moment("2016-01-17T:08:44:29+0100").format('MM/DD/YYYY');
for more format option check Moment.format()
Or if you prefer, {{today | date:'fullDate'}}
– Amit kumar Jan 15 '17 at 08:34Have a look at this answer
You can create a new Date("2016-01-17T08:44:29+0100") //removed a colon
object and then get the month, day, year, hours, minutes, and seconds by extracting them from the Date
object, and then create your string. See snippet:
const date = new Date("2016-01-17T08:44:29+0100"); // had to remove the colon (:) after the T in order to make it work
const day = date.getDate();
const monthIndex = date.getMonth();
const year = date.getFullYear();
const minutes = date.getMinutes();
const hours = date.getHours();
const seconds = date.getSeconds();
const myFormattedDate = day+"-"+(monthIndex+1)+"-"+year+" "+ hours+":"+minutes+":"+seconds;
document.getElementById("dateExample").innerHTML = myFormattedDate
<p id="dateExample"></p>
It is not the most elegant way, but it works.
Check if this is helpful.
var reTime = /(\d+\-\d+\-\d+)\D\:(\d+\:\d+\:\d+).+/;
var originalTime = '2016-01-17T:08:44:29+0100';
var newTime = originalTime.replace(this.reTime, '$1 $2');
console.log('newTime:', newTime);
Output:
newTime: 2016-01-17 08:44:29