Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Get the Timestamp in JavaScript?
Next article icon

How to Get Current Time in JavaScript ?

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This article will show you how to get the current time in JavaScript. Whether you are creating a digital clock, scheduling events, or simply logging timestamps, knowing how to retrieve the current time is essential.

Here, we will cover different methods to get the current time in JavaScript.

Table of Content

  • Using the Date Object
  • Extracting Time Components
  • Formatting the Time
  • Using toLocaleTimeString()

Using the Date Object

The Date object in JavaScript is used to work with dates and times. You can get the current date and time by creating a new instance of the Date object.

Example: This example shows the use of the above-mentioned approach.

JavaScript
let currentTime = new Date(); console.log(currentTime); 

Output
2024-03-12T07:14:22.217Z 

This will output the current date and time in the default format, which includes the full date along with the time.

Extracting Time Components

You can extract specific components of the time, such as hours, minutes, and seconds, using the getHours(), getMinutes(), and getSeconds() methods of the Date object. This will output the current time in the format "Hours:Minutes:Seconds".

Example: This example shows the use of the above-mentioned approach.

JavaScript
let now = new Date(); let hours = now.getHours(); let minutes = now.getMinutes(); let seconds = now.getSeconds();  console.log(`Current Time: ${hours}:${minutes}:${seconds}`); 

Output
Current Time: 7:14:22 

Formatting the Time

You might want to format the time in a specific way, such as ensuring that minutes and seconds are always displayed with two digits. You can achieve this by adding a leading zero to single-digit numbers.

Example: This example shows the use of the above-mentioned approach.

JavaScript
function formatTime(number) {     return number < 10 ? '0' + number : number; }  let now = new Date(); let hours = formatTime(now.getHours()); let minutes = formatTime(now.getMinutes()); let seconds = formatTime(now.getSeconds());  console.log(`Current Time: ${hours}:${minutes}:${seconds}`); 

Output
Current Time: 07:14:21 

In this example, the formatTime function adds a leading zero to numbers less than 10. This ensures that the time is always displayed in a consistent format, even when minutes or seconds are single digits.

Using toLocaleTimeString()

The toLocaleTimeString() method returns a string with a language-sensitive representation of the time portion of the specified date. This method is quite flexible and allows you to specify different options to format the time according to your needs.

Example: This example shows the use of the toLocaleTimeString() method.

JavaScript
let now = new Date(); let currentTime = now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit' });  console.log(`Current Time: ${currentTime}`); 

Output
Current Time: 09:56:03 AM 



Next Article
How to Get the Timestamp in JavaScript?

V

vkash8574
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to Get the Current Date in JavaScript ?
    The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string. Syntax dateObj.toDateString()[GFGTABS] JavaScript // Crea
    2 min read
  • How to Log the Current Date in JavaScript ?
    The Date object is an inbuilt object constructor of JavaScript language. It is used to work with dates and times. The Date object is created by using the new keyword, i.e. new Date(). The current date can be logged on the console in JavaScript using the below-discussed methods. Table of Content Usin
    3 min read
  • How to get the current year using JavaScript ?
    In this article, we will know how to get the current year using the build-in method in Javascript. The purpose is to get the current year by using JavaScript getFullYear() method that will return the full year in 4-digit format for some specified date. This method is used to fetch the year from a gi
    1 min read
  • How to Get the Timestamp in JavaScript?
    A timestamp is a numeric representation of the current time. It is a unique identifier that marks the exact moment when an event occurred or when a certain action was performed. 1. Using Date.now() MethodThis approach uses Date.now() method. This method returns the number of milliseconds since Janua
    2 min read
  • How to get the client timezone offset in JavaScript?
    The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes. This offset is changed by dividing by 60 and negating the result. Not
    1 min read
  • JavaScript - How To Create A Countdown Timer?
    A countdown timer is a timer that runs for a specific time limit and when that limit get crossed then it stops the timer and the message get displayed on the screen. it can be used for a website or blog to display the countdown to any special event, such as a birthday or anniversary. The Basics of a
    4 min read
  • How to Get Current time in Golang?
    With the help of time.Now() function, we can get the current time in Golang by importing time module. Syntax: time.Now() Return: Return current date and time. Example #1: In this example, we can see that by using time.Now() function, we are able to get the current date and time. // Golang program to
    1 min read
  • How to Get Milliseconds in JavaScript?
    In JavaScript, you can get the current time in milliseconds, which is useful for timestamping events, measuring time intervals, and performing time calculations. JavaScript provides various methods to precisely measure time down to the millisecond, including Date.now(), performance.now(), and the Da
    2 min read
  • How to get seconds since epoch in JavaScript?
    Given a date, we have to find the number of seconds since the epoch (i.e. 1 January 1970, 00:00:00 UTC ). The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number o
    1 min read
  • How to Get Current Formatted Date dd/mm/yyyy in JavaScript?
    Here are different ways to get the current date in dd/mm/yyyy format. 1. Using Native JavaScript MethodsJavaScript's built-in Date object provides various methods to work with dates and times. You can extract individual components like day, month, and year to format the date manually. [GFGTABS] Java
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences