How to get the current time in JS
In JavaScript, getting the current time is a common operation, whether for logging, timestamp generation, or dynamic display of time. This article will introduce in detail how to use JavaScript to obtain the current time and provide structured data for quick reference.
1. Use Date object to get the current time

JavaScript provides built-inDateObject for working with dates and times. Here's the basic way to get the current time:
| method | Description | Example |
|---|---|---|
| newDate() | Create a Date object containing the current date and time | const now = new Date(); |
| Date.now() | Returns the timestamp of the current time in milliseconds | const timestamp = Date.now(); |
2. Get various parts of time
PassDateThe method of the object can obtain the year, month, day, hour, minute, second and other components of the time:
| method | Description | Example |
|---|---|---|
| getFullYear() | Get the year (4 digits) | now.getFullYear(); // 2023 |
| getMonth() | Get the month (0-11) | now.getMonth(); // 0 means January |
| getDate() | Get date (1-31) | now.getDate(); // 15 |
| getHours() | Get the hour (0-23) | now.getHours(); // 14 |
| getMinutes() | Get minutes (0-59) | now.getMinutes(); // 30 |
| getSeconds() | Get seconds (0-59) | now.getSeconds(); // 45 |
3. Format the current time
JavaScript does not have a built-in method for formatting dates, but it can be achieved by combining the methods of the Date object:
| Formatting requirements | Implementation method | Example output |
|---|---|---|
| YYYY-MM-DD | Splice year, month, day | 2023-01-15 |
| HH:MM:SS | Splice hours, minutes, seconds | 14:30:45 |
| YYYY-MM-DD HH:MM:SS | Combine date and time | 2023-01-15 14:30:45 |
4. Use toLocaleString method
JavaScript providestoLocaleStringA series of methods that can format dates and times according to localization settings:
| method | Description | Example |
|---|---|---|
| toLocaleDateString() | Returns a localized date string | 2023/1/15 |
| toLocaleTimeString() | Returns the localized time string | 2:30:45 PM |
| toLocaleString() | Returns a localized date and time string | 2023/1/15 2:30:45 pm |
5. Third-party library recommendations
For more complex datetime processing, third-party libraries can be used:
| library name | Features | Installation method |
|---|---|---|
| Moment.js | Powerful, but bulky | npm install moment |
| date-fns | Modular design, introduced on demand | npm install date-fns |
| Day.js | Lightweight, API compatible with Moment | npm install dayjs |
6. Practical application examples
Here is a complete example showing how to get and format the current time:
| Function | Code implementation |
|---|---|
| Get current time | const now = new Date(); |
| Format date | const dateStr = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`; |
| Format time | const timeStr = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`; |
| Combined output | console.log(`Current time: ${dateStr} ${timeStr}`); |
With the above methods, you can easily get and manipulate the current time in JavaScript. Depending on project needs, you can choose native methods or third-party libraries to implement more complex time processing functions.
check the details
check the details