JavaScript >> Javascript 文檔 >  >> Tags >> Date

比較 JavaScript 日期(日/分/小時/之前/之後),並帶有示例

繼我們關於在 JavaScript 中從日期中添加和減去時間的文章之後 - 以下是比較兩個 JavaScript 日期對象的方法。

本文將探討比較不同粒度級別的日期/時間——從確切的月份日期到同一年的日期。

使用布爾運算符

原生 Javascript 日期對象可以使用標準布爾/比較運算符進行比較 - 允許您通過檢查日期是否等於、不等於、大於、小於、等於或大於、或等於或小於來比較日期 彼此。

在 Javascript 中檢查日期是否相同

檢查是否相等:

var date1 = new Date(); // Defaults to now
var date2 = new Date(); // Defaults to now

if(date1 == date2) {
    // Dates are the same
}

檢查一個日期是在另一個日期之前還是之後

只需使用標準的 JavaScript 布爾比較運算符:

var date1 = new Date(); // Defaults to now
var date2 = new Date(); // Defaults to now

date1.setTime(date1.getTime() + (1 * 60 * 60 * 1000));  // Add an hour to date1

if(date1 > date2){
    // The time stored by date1 is after date2
}

if(date2 < date1){
    //The time stored by date2 is before date1
}

檢查兩個日期對像是否共享相同的秒、分、小時、日、月、年

當對日期對象使用布爾比較時,它們被比較到毫秒(1/1000 秒)。以下是與不同時間單位進行比較的方法:

var date1 = new Date(); // Defaults to now
var date2 = new Date(); // Defaults to now

date1.setTime(date1.getTime() + (1 * 60 * 60 * 1000));  // Add an hour to date1


# The getSeconds() method will return the seconds component of the date object from 0-59

# The getMinutes() method will return the minutes component of the date object from 0-59

# The getHours() method will return the hours component of the date object from 0-23

# The getDate() method will return the date (day in month) of the date object from 0-31

# The getMonth() method will return the month of the date object from 0-11 (Starting with January at 0)

# The getFullYear() method will return the year of the date object as a 4 digit number (eg 2021)

#So, to see if two dates share the same minute you could run

if(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate() && date1.getHours() == date2.getHours() && date1.getMinutes() == date2.getMinutes() && ){
    // Dates are the same down to the minute
}

時刻 JS

上面的最後一個例子有點亂——有更好的方法。

如果您構建的應用程序經常處理日期,那麼 Moment.js 是無價的。

Moment.js 提供了管理日期、時區、時間段(兩個日期之間的時間段)的工具——所有這些都方便地封裝在易於使用的類中。文檔很棒,它簡化了日期處理,同時提高了可靠性。

在以下位置找到它:

https://momentjs.com/

例如,檢查兩個日期是否在同一分鐘內,您只需運行:

date1 = moment(); // Defaults to now
date2 = moment().add(7, 'days').add(2, 'minutes'); // Second date is 7 days and 2 minutes into the future

date1.isSame(date2, 'minute'); // Returns true if the dates are the same down to the minute


Tutorial JavaScript 教程
  1. 從 JavaScript 讀取 Facebook 應用程序 Cookie?

  2. 7 月 25 日星期四加入我們,參加 Bitovi 的在線聚會

  3. 將加密貨幣支付添加到 React Todo 應用程序(或我如何讓我的孩子做他們的家務)

  4. 在 React 中使用 Formik 和 Yup 進行表單驗證

  5. 如何使 Angular CLI 構建的應用程序從子文件夾中工作

  6. 正則表達式 101

  7. JavaScript、Ruby 和 C 不是通過引用調用的

  1. 在本地系統上設置 TypeScript Playground

  2. Ember 和 Ember CLI 入門

  3. 我使用 VueJS 製作了一個西蒙遊戲變體🎮

  4. 在 javascript 正則表達式中選擇任何符號

  5. vue.js 開發者(初學者)的問題

  6. 如何使用 es6 在 react-native 中初始化數組?

  7. JavaScript 基礎:變量

  1. 使用 Svelte 進行可見性檢測

  2. 使用 Redux 工具包的現代 React Redux 教程 - 2020

  3. 使用 Node.js 保護 Github Webhook

  4. 結合 VuePress 和 Bootstrap 創建一個文檔站點並將其部署到 Netlify