Navigate back to the homepage

How to get current time in typescript?

Infinitbility
TypeScript
February 6th, 2022 · 1 min read
keep affilitate

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

typescript, current, time, example
TypeScript, get current time example.

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.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get current date in typescript?

TypeScript, get current date example.

February 5th, 2022 · 1 min read

How to get array from json object in typescript?

TypeScript, get array from object example.

February 4th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]