23

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?

wonea
  • 3,987
  • 17
  • 71
  • 134
Protagonist
  • 1,390
  • 6
  • 26
  • 49

4 Answers4

12

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()

clearlight
  • 10,772
  • 11
  • 48
  • 65
Amit kumar
  • 5,341
  • 5
  • 23
  • 38
  • 1
    I cannot use moment.js – Protagonist Jan 15 '17 at 08:33
  • for html part you can do using pipe

    Or if you prefer, {{today | date:'fullDate'}}

    – Amit kumar Jan 15 '17 at 08:34
  • I'm using some third party module for UI and I cannot format in HTML. So i have to handle the format in typescript and send it for rendering – Protagonist Jan 15 '17 at 08:35
  • if you want to do it just using ts then first extract all day,month,year then concat them in string . i use mostly moment.js. may i know why can't you use moment.js ? @Protagonist – Amit kumar Jan 15 '17 at 08:36
  • Correct. I know how to concat. But how to extract the date and time into strings? – Protagonist Jan 15 '17 at 08:39
  • typescript is superset of javascript. so every date method will work in typescript. just extract getDay() month() in a variable and in new variable just add all of them like var stringdate=day+'/'+month+'/'+year. in stringdate var you will get string format of you date – Amit kumar Jan 15 '17 at 08:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133184/discussion-between-amit-suhag-and-protagonist). – Amit kumar Jan 15 '17 at 08:47
9

Have 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:

var date = new Date("2016-01-17T08:44:29+0100"); // had to remove the colon (:) after the T in order to make it work
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
var minutes = date.getMinutes();
var hours = date.getHours();
var seconds = date.getSeconds();
var 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.

John
  • 6,370
  • 4
  • 37
  • 58
  • I'm getting "Object expected" error at var myFormattedDate =day+........; and Im using typescript – Protagonist Jan 15 '17 at 09:41
  • Did you try to run the snippet? You also have to get rid of the first colon in your api format in order to make a valid Date object – John Jan 15 '17 at 10:23
  • You can remove the first colon by using the `replace()`-method like this: `var myStr = "2016-01-17T08:44:29+0100".replace(":","");` – John Jan 15 '17 at 11:07
  • My bad.. there was no colon in the input. Im getting object expected error if I do console.log right after let day= date.getDate(); I'm using typescript – Protagonist Jan 15 '17 at 11:27
  • It should work as it is, but I do not know how you execute your code. You could try to write `var date:Object = new Date("2016-01-17T08:44:29+0100");` Adding an Object type to date. – John Jan 15 '17 at 12:10
  • There is no mucg difference between var and let except scope. I tried with let day: Date = date.getDate(); but no luck – Protagonist Jan 15 '17 at 12:43
  • You should not write `let day: Date = date.getDate();`, but `let date:Date = new Date("2016-01-17T08:44:29+0100");` the day is a `number`. date is a `Date`. Also try to use `Object` as a type instead of `Date` if it does not compile – John Jan 15 '17 at 12:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133193/discussion-between-protagonist-and-john). – Protagonist Jan 15 '17 at 12:56
  • you need to use parentesis when adding 1 to month: return day+"-"+(monthIndex+1)+"-"+year+" "+ hours+":"+minutes+":"+seconds; – sourcerebels Sep 18 '17 at 13:32
  • 1
    @sourcerebels thank you for pointing that out. I have updated my answer accordingly – John Sep 18 '17 at 13:39
1

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
Angel T
  • 41
  • 3
0

new Date().toLocaleString()

output:

"31/03/2020 ,00:00:0"

(now,your output is string)

I'm
  • 53
  • 6