2

I am using angular 2/4 How can i format the date into this
2017-10-03T14:51:06.078Z

here is my code below :

  public today: number = Date.now();
 console.log("today time " +this.today);

I tried to add

this.today.toLocaleString()

that didn't seem to work

2

2 Answers 2

5

You can do this something like

this.todayDate = new Date();
this.dateToday = (this.todayDate.getFullYear() + '-' + ((this.todayDate.getMonth() + 1)) + '-' + this.todayDate.getDate() + ' ' +this.todayDate.getHours() + ':' + this.todayDate.getMinutes()+ ':' + this.todayDate.getSeconds());

You can check with console log,

console.log('today date', this.dateToday);

The output will be

today date 2017-10-3 10:38:10

Explaination: "this.dateToday" is a variable of type any which will store the date as you want.

You need to get individual year, month, date and the time as well and concat it together. There are many more date methods you can check online or in your IDE.

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

Comments

4

You should use the toISOString method:

this.today.toISOString();

But you need to change the type from number to Date:

namespace Testing {
    export class Test {
        public today: Date;

        constructor() {
            this.today = new Date();
            console.log(this.today.toISOString());
        }
    }
}

More information can be found here

2 Comments

I tried your code it didnt work console.log("today time " +this.today.toISOString()); I get error
Did you make today of the right type? And what compile error do you get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.