Welcome to visit Kuiju!
Current location:front page >> educate

How to get the current time in js

2025-12-13 15:06:36 educate

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

How to get the current time in js

JavaScript provides built-inDateObject for working with dates and times. Here's the basic way to get the current time:

methodDescriptionExample
newDate()Create a Date object containing the current date and timeconst now = new Date();
Date.now()Returns the timestamp of the current time in millisecondsconst 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:

methodDescriptionExample
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 requirementsImplementation methodExample output
YYYY-MM-DDSplice year, month, day2023-01-15
HH:MM:SSSplice hours, minutes, seconds14:30:45
YYYY-MM-DD HH:MM:SSCombine date and time2023-01-15 14:30:45

4. Use toLocaleString method

JavaScript providestoLocaleStringA series of methods that can format dates and times according to localization settings:

methodDescriptionExample
toLocaleDateString()Returns a localized date string2023/1/15
toLocaleTimeString()Returns the localized time string2:30:45 PM
toLocaleString()Returns a localized date and time string2023/1/15 2:30:45 pm

5. Third-party library recommendations

For more complex datetime processing, third-party libraries can be used:

library nameFeaturesInstallation method
Moment.jsPowerful, but bulkynpm install moment
date-fnsModular design, introduced on demandnpm install date-fns
Day.jsLightweight, API compatible with Momentnpm install dayjs

6. Practical application examples

Here is a complete example showing how to get and format the current time:

FunctionCode implementation
Get current timeconst now = new Date();
Format dateconst dateStr = `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`;
Format timeconst timeStr = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
Combined outputconsole.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.

Next article
  • How to get the current time in JSIn 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 timeJavaScript provides built-inDateObject for working with dates and times. Her
    2025-12-13 educate
  • How about the J1900 processor? In-depth analysis of hot topics and performance across the entire networkRecently, with the growth of the second-hand market and the demand for mini-hosts, the Intel J1900 processor has once again become the focus of heated discussion. This article combines the discussion data of the entire network in the past 10 days to comprehensively analyze the performance of this classic processor
    2025-12-11 educate
  • How to remove seal inkIn daily life, it is a common problem for stamp ink to accidentally contaminate clothing, paper or tabletops. How to effectively remove these stubborn ink marks? This article will provide you with structured solutions based on hot topics and hot content from the entire network in the past 10 days.1. Ranking of popular removal methods (data in the past 10 days)methodFrequency of useApplicable mat
    2025-12-08 educate
  • How to miscarry when pregnant with an upper ringIn recent years, with the popularity of contraceptive measures, the intrauterine device (IUD) has become one of the contraceptive methods chosen by many women. However, the contraceptive ring is not 100% effective, and a very small number of women may become pregnant after having the ring inserted. In view of this situation, many women are concerned about the issue of "
    2025-12-06 educate
Recommended articles
Reading rankings
Friendly links
Dividing line