29

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?

5
  • are you talking about just to display on UI if yes then I post my answer . Commented Jan 15, 2017 at 8:29
  • 1
    @YashveerSingh No. I want to format in typescript, not with pipes Commented Jan 15, 2017 at 8:32
  • ok sorry I thought otherwise I will remove my answer . Let me check about how to do it in typescript . Commented Jan 15, 2017 at 8:34
  • @YashveerSingh 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 Commented Jan 15, 2017 at 8:36
  • typescript it self provide some methods like to new Date().toLocaleString() . have you tried all available ? I am trying them now if it work will post the answer . Commented Jan 15, 2017 at 8:39

4 Answers 4

17

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

Sign up to request clarification or add additional context in comments.

8 Comments

I cannot use moment.js
for html part you can do using pipe <p>Or if you prefer, {{today | date:'fullDate'}}</p>
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
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
Correct. I know how to concat. But how to extract the date and time into strings?
|
11

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:

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.

12 Comments

I'm getting "Object expected" error at var myFormattedDate =day+........; and Im using typescript
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
You can remove the first colon by using the replace()-method like this: var myStr = "2016-01-17T08:44:29+0100".replace(":","");
@sourcerebels thank you for pointing that out. I have updated my answer accordingly
@JoopAué getDay will give you a number between 0 and 6, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, not the actual date.
|
5

new Date().toLocaleString()

output:

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

(now,your output is string)

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.