Home  >  Article  >  Web Front-end  >  How to output the date written in javascript

How to output the date written in javascript

王林
王林Original
2023-05-17 15:07:081547browse

JavaScript is a commonly used scripting language that is widely used in website development, mobile applications, and desktop applications. In JavaScript, date is a common data type that can be used to store and manipulate dates and times. This article will introduce how to use JavaScript to output dates.

1. Get the current date

To get the current date, you can use JavaScript’s Date object. The Date object has a parameterless constructor that returns the current date and time. The following is a simple example:

var today = new Date();
console.log(today);

After executing the above code, the current date and time will be output on the console in the following format:

Thu Jan 28 2021 11:26:24 GMT+0800 (中国标准时间)

The above output format may not be what we want. We can use the methods of the Date object to format date output. The following are some commonly used methods:

1. getFullYear(): Get the year.

var year = today.getFullYear();
console.log(year);

2. getMonth(): Get the month (0 means January, 11 means December).

var month = today.getMonth() + 1;
console.log(month);

3. getDate(): Get the date (1-31).

var date = today.getDate();
console.log(date);

4. getDay(): Get the day of the week (0 means Sunday, 6 means Saturday).

var day = today.getDay();
console.log(day);

5. getHours(): Get the number of hours (0-23).

var hours = today.getHours();
console.log(hours);

6. getMinutes(): Get the number of minutes (0-59).

var minutes = today.getMinutes();
console.log(minutes);

7. getSeconds(): Get the number of seconds (0-59).

var seconds = today.getSeconds();
console.log(seconds);

2. Formatted date output

The above method can obtain different date and time parts, and we can splice them together to achieve a customized date format. The following is an example:

var year = today.getFullYear();
var month = today.getMonth() + 1;
var date = today.getDate();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();

var formattedDate = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;
console.log(formattedDate);

After executing the above code, the date in the following format will be output on the console:

2021-1-28 11:32:47

If you want to display a more precise time, you can use the following method:

8. getMilliseconds(): Get the number of milliseconds (0-999).

var milliseconds = today.getMilliseconds();
console.log(milliseconds);

After using the above method, you can splice out a more accurate date and time output.

3. Custom date

In addition to getting the current date, we can also create a custom date object based on the given year, month, day and other parameters. The following is an example:

var customDate = new Date(2021, 0, 1, 12, 0, 0, 0);
console.log(customDate);

The above code creates a custom date object, representing 12 o'clock on January 1, 2021. Note that months start counting from 0, so 0 means January.

Use the above method to output the date in JavaScript. With date objects and date formatting methods, we can flexibly process and display dates to meet different needs.

The above is the detailed content of How to output the date written in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn