Javascript 简明教程

JavaScript - Get Date Methods

Get Date Methods

JavaScript 利用 Date 对象进行日期和时间操作。此对象提供了一系列方法,以方便检索和修改特定于日期的信息。这里,我们将讨论 JavaScript 中的获取日期方法,这些方法用于获取日期/时间的不同组件。

JavaScript utilizes the Date object for date and time manipulation. This object offers an array of methods that facilitate retrieval and modification of date-specific information. Here, we will discuss about the get date methods within JavaScript which fetch different components of the date/time.

下面是表中使用最广泛的获取日期方法及其相应的描述。

Below is a table of the most commonly used get date methods and their corresponding description.

Method

Description

getFullYear()

This method fetches and presents the comprehensive calendar year by retrieving the current year in local time zone; it returns the full four-digit representation of a local date object.

getMonth()

Returns the month (0-11) of the local date object. This method retrieves the current month, with values ranging from 0 (January) to 11 (December). It’s useful for displaying and manipulating month-related information.

getDate()

The method: 'returns the day component of the current date', a value ranging from 1 to 31. This functionality proves particularly useful when one needs this information extracted from a local date object.

getHours()

The function 'getHours()' extracts and returns the local date object’s hour component (0-23). This allows you to retrieve the current hour in your local time zone for a variety of time-related applications.

getMinutes()

Returns the minutes (0-59) of the local date object. Retrieves the current minute component, ranging from 0 to 59. Useful for displaying and handling time-related data at the minute level.

getSeconds()

This returns the seconds ranging from 0 to 59 of the local date object. It provides precision down the seconds for a variety of time-based calculations/displays.

getMilliseconds()

Returns the milliseconds (0-999) of the local date object. Retrieves the current millisecond component, allowing for high precision in time-related applications and calculations.

getDay()

Returns the index of day of the week starting from 0 which stands for Sunday, all the way up to 6 for Saturday.

getUTCFullYear()

Returns the full 4-digit year of the date object in Coordinated Universal Time (UTC). This method retrieves the current year in UTC, providing a standardized representation of the calendar year irrespective of the local time zone.

getUTCMonth()

Returns the index of the month ranging from 0(Jan) to 11(Dec) but of the date object in Coordinated Universal Time (UTC).

getUTCDate()

Returns the day of the month (1-31) of the date object in Coordinated Universal Time (UTC). Useful for obtaining the day component of the current date in a UTC context.

getUTCHours()

Returns the hour (0-23) of the date object in Coordinated Universal Time (UTC). Retrieves the current hour in UTC, allowing for standardized access to the hour component across different time zones.

getUTCMinutes()

Returns the minutes (0-59) of the date object in Coordinated Universal Time (UTC). Retrieves the current minute component in UTC, providing standardized minute information for various international time-based applications.

getUTCSeconds()

The function fetches the seconds (ranging from 0 to 59) of a date object in Coordinated Universal Time (UTC). It also acquires the current second component in UTC, thereby enabling standardized second information across various time zones.

getUTCMilliseconds()

The function returns the milliseconds (0-999) of the date object in Coordinated Universal Time (UTC); it retrieves and offers high precision for standardized time-related calculations and applications: specifically, it provides the current millisecond component in UTC.

Examples

Example 1: Simple demonstration of get date methods

以下示例演示了流行的 JavaScript 日期方法的基本应用程序:它实例化一个新的日期对象来表示当前日期和时间;随后,它展示了各种组件(年、月、日;小时、分钟、秒、毫秒)的数组以及它们相应的 UTC 对应物。显示的元素不仅包含标准的时间划分,还包含关于周几的补充信息:从而提供深入了解当前时间动态。

The following example demonstrates the fundamental application of prevalent JavaScript date methods: It instantiates a novel Date object to represent the present date and time; subsequently, it exhibits an array of diverse components - year, month, day; hours, minutes, seconds, milliseconds; along with their corresponding UTC counterparts. The displayed elements encompass not only standard temporal divisions but also supplementary information about weekdays: thus providing comprehensive insight into current temporal dynamics.

<!DOCTYPE html>
<html>
<head>
   <title> Exxample to demonstrate get date methods in JavaScript</title>
</head>
<body>
   <script>
      // Create a new Date object
      const currentDate = new Date();

      function displayResult(methodName, result) {
         const resultDiv = document.createElement('div');
         resultDiv.innerHTML = `${methodName}: ${result}`;
         document.body.appendChild(resultDiv);
      }

      displayResult('getFullYear()', currentDate.getFullYear());
      displayResult('getMonth()', currentDate.getMonth());
      displayResult('getDate()', currentDate.getDate());
      displayResult('getHours()', currentDate.getHours());
      displayResult('getMinutes()', currentDate.getMinutes());
      displayResult('getSeconds()', currentDate.getSeconds());
      displayResult('getMilliseconds()', currentDate.getMilliseconds());
      displayResult('getDay()', currentDate.getDay());
      displayResult('getUTCFullYear()', currentDate.getUTCFullYear());
      displayResult('getUTCMonth()', currentDate.getUTCMonth());
      displayResult('getUTCDate()', currentDate.getUTCDate());
      displayResult('getUTCHours()', currentDate.getUTCHours());
      displayResult('getUTCMinutes()', currentDate.getUTCMinutes());
      displayResult('getUTCSeconds()', currentDate.getUTCSeconds());
      displayResult('getUTCMilliseconds()', currentDate.getUTCMilliseconds());
   </script>
</body>
</html>

Example 2: Comparison of two dates

在此示例中,Date 构造函数创建两个特定日期:date1 和 date2。脚本随后比较这些日期;它显示它们的格式化表示,以及指示 date1 是晚于、早于还是等于 date2 的消息。

In this example, the Date constructor creates two specific dates: date1 and date2. The script subsequently compares these dates; it displays their formatted representations, along with a message indicating if date1 is later, earlier or equal to date2.

<!DOCTYPE html>
<html>
<head>
   <title>Comparison of two dates in JavaScript</title>
</head>
<body>
   <script>
      const date1 = new Date(2024, 0, 18);
      const date2 = new Date(2024, 0, 26);

      function displayComparison() {
         const date1Div = document.createElement('div');
         date1Div.innerHTML = `Date 1: ${date1.toDateString()}`;
         document.body.appendChild(date1Div);

         const date2Div = document.createElement('div');
         date2Div.innerHTML = `Date 2: ${date2.toDateString()}`;
         document.body.appendChild(date2Div);

         const resultDiv = document.createElement('div');
         if (date1 > date2) {
            resultDiv.innerHTML = "date 1 is later than date 2";
         } else if (date1 < date2) {
            resultDiv.innerHTML = "date 1 is earlier than date 2";
         } else {
            resultDiv.innerHTML = "Both dates are equal";
         }
         document.body.appendChild(resultDiv);
      }

      displayComparison();
   </script>
</body>
</html>