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 correct rib valgus in childrenIn recent years, the problem of rib valgus in children has gradually attracted the attention of parents. Rib valgus means that the lower edge of a child's ribs tilts outward, which may be caused by calcium deficiency, poor posture or congenital factors. This article will provide you with structured solutions based on popular discussions across the Internet in the past 10 days.1. C
    2026-01-29 educate
  • How to make delicious goose bloodIn the past 10 days, among the hot topics about food on the Internet, the cooking method of goose blood has become the focus of many netizens. Goose blood is not only rich in nutrients, but also has a unique taste and is suitable for a variety of cooking methods. This article will give you a detailed introduction to the purchase, processing and cooking methods of goose blood based on
    2026-01-27 educate
  • Why does my back often hurt?Back pain is one of the common health problems of modern people, which may be related to factors such as lifestyle habits, working postures, and diseases. The following is a detailed analysis of back pain, combined with hot health topics in the past 10 days, to provide you with structured data and solutions.1. Common causes of back painCause classificationSpecific performanceHigh-risk grou
    2026-01-24 educate
  • How to deal with wrinkled organza: popular methods and practical tips on the InternetRecently, the topic of "how to deal with wrinkled organza" has soared on social media and e-commerce platforms. Especially with the popularity of light fabric clothing in summer, the maintenance of organza has become the focus of consumers' attention. The following are the solutions and data analysis that have been hotly discussed on
    2026-01-22 educate
Recommended articles
Reading rankings
Friendly links
Dividing line