Hello Friends 👋,
Welcome To Infinitbility! ❤️
To get the current datetime in typescript has the Date()
object it will return every time the current date and time when we use it.
we have to just write new Date()
and it returns the date object where we get a year, month, date, hours, minutes, and seconds.
1let today: object = new Date();2console.log(today); // 2022-02-06T08:05:49.292Z
Now, we know we have the current date time but we can’t show the same to also users.
To format, we have multiple packages like Day.js, and Moment.js.
But if wanna follow a custom formater follow the below code.
1function getCurrentTime() {2 let today: object = new Date();3 let hours: number = (today.getHours() < 10 ? '0' : '') + today.getHours();4 let minutes: number = (today.getMinutes() < 10 ? '0' : '') + today.getMinutes();5 let seconds: number = (today.getSeconds() < 10 ? '0' : '') + today.getSeconds();6 return hours + ':' + minutes + ':' + seconds;7}8console.log(getCurrentTime()); // '15:08:42'
Output

In above example, we are extracting and formating new Date()
to time in typescript.
Thanks for reading…
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.