<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Moment.js 教程 > 原文: [http://zetcode.com/javascript/momentjs/](http://zetcode.com/javascript/momentjs/) Moment.js 教程展示了如何通過 Moment.js 模塊在 JavaScript 中使用日期和時間。 ## Moment.js Moment.js 是一個輕量級的 JavaScript 日期庫,用于解析,驗證,操作和格式化日期。 在本教程中,我們在 Node 應用中使用 Moment.js。 [Day.js 教程](/javascript/dayjs/)中介紹了類似的 Day.js 庫。 ## 安裝 Moment.js 首先,我們安裝 Moment.js。 ```js $ nodejs -v v9.11.2 ``` 我們使用 Node 版本 9.11.2。 ```js $ npm init ``` 我們啟動一個新的 Node 應用。 ```js $ npm i moment ``` 我們使用`npm i moment`命令安裝 Moment.js。 ## Moment.js 今天的日期 在第一個示例中,我們使用 Moment.js 獲取今天的日期。 `today.js` ```js const moment = require('moment'); let now = moment(); console.log(now.format()); ``` 該示例打印今天的日期和時間。 ```js const moment = require('moment'); ``` 我們加載 Moment.js 庫。 ```js let now = moment(); ``` 我們使用`moment()`獲得當前本地日期時間對象。 ```js console.log(now.format()); ``` 我們使用`format()`格式化輸出。 默認情況下,我們使用長日期時間格式。 ```js $ node today.js 2018-07-01T16:32:53+02:00 ``` 這是 ISO 標準格式。 日期和時間部分用 T 字符分隔。 該字符串以時區結尾。 ## 創建 Moment.js 對象 我們可以使用幾種方法來創建日期和時間 Moment.js 對象。 這些對象必須稍后再格式化為人類可讀的格式。 `create_objects.js` ```js const moment = require('moment'); let d1 = moment("2018-06-03"); console.log(d1.format('ll')); let d2 = moment([2017, 11, 23]); console.log(d2.format('ll')); let d3 = moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123}); console.log(d3.format('ll')); let d4 = moment(1530471537000); console.log(d4.format('ll')); let d5 = moment(new Date(2011, 11, 22)); console.log(d5.format('ll')); ``` 該示例以五種不同方式創建日期和時間對象。 ```js let d1 = moment("2018-06-03"); ``` 我們從字符串創建一個矩對象。 ```js let d2 = moment([2017, 11, 23]); console.log(d2.format('ll')); ``` 在這里,從數組創建一個矩對象。 ```js let d3 = moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123}); console.log(d3.format('ll')); ``` 我們可以使用 JSON 對象創建力矩對象。 ```js let d4 = moment(1530471537000); console.log(d4.format('ll')); ``` 我們使用 unix 時間戳(以毫秒為單位)定義矩對象。 ```js let d5 = moment(new Date(2011, 11, 22)); console.log(d5.format('ll')); ``` 最后,我們使用 JavaScript 內置的 Date 對象定義一個矩對象。 ```js $ node create_moment_objects.js Jun 3, 2018 Dec 23, 2017 Apr 5, 2010 Jul 1, 2018 Dec 22, 2011 ``` 這是輸出。 ## Moment.js 格式化日期時間 Moment.js 對象使用`format()`函數格式化。 也有本地化格式的選項。 `format.js` ```js const moment = require('moment'); let now = moment(); console.log("ISO") console.log(now.format()); console.log("\nTime") console.log(now.format("HH:mm:ss")); console.log(now.format("h:mm:ss a")); console.log("\nDate") console.log(now.format("dddd, MMMM Do YYYY")); console.log(now.format("YYYY-MM-DD")); console.log("\nLocalized") console.log(now.format("LT")); console.log(now.format("LTS")); console.log(now.format("LTS")); console.log(now.format("L")); console.log(now.format("l")); ``` 該示例使用 Moment 的`format()`函數格式化日期和時間。 ```js $ node format.js ISO 2018-07-03T10:09:47+02:00 Time 10:09:47 10:09:47 am Date Tuesday, July 3rd 2018 2018-07-03 Localized 10:09 AM 10:09:47 AM 10:09:47 AM 07/03/2018 7/3/2018 ``` 這是一個示例輸出。 ## Moment.js 計算日期時間差 使用`diff()`函數,我們可以計算兩個日期時間對象之間的差。 `difference.js` ```js const moment = require('moment'); let d1 = moment('2018-06-12'); let d2 = moment('2018-06-28'); let days = d2.diff(d1, 'days'); console.log(`Difference in days: ${days}`); let hours = d2.diff(d1, 'hours'); console.log(`Difference in hours: ${hours}`); ``` 該示例以天和小時為單位計算兩個矩對象之間的差異。 ```js let days = d2.diff(d1, 'days'); ``` 第二個參數表明輸出將以天為單位。 ```js $ node difference.js Difference in days: 16 Difference in hours: 384 ``` 這是輸出。 Borodino 戰役是 1812 年 9 月 7 日在法國入侵俄羅斯期間的拿破侖戰爭中進行的戰斗。 `borodino.js` ```js const moment = require('moment'); let borodinoBattle = moment('1812-09-07'); let now = moment(); let days = now.diff(borodinoBattle, 'days'); console.log(`On ${now.format('ll')}, ${days} days have passed since the Borodino battle.`); ``` 在此示例中,我們計算了從那時起經過的天數。 ```js $ node borodino.js On Jul 3, 2018, 75174 days have passed since the Borodino battle. ``` 這是一個示例輸出。 ## Moment.js 日期時間算法 `add()`函數用于將日期和時間添加到矩對象,而`subtract()`函數用于從矩對象減去日期和時間。 `add_sub.js` ```js const moment = require('moment'); let now = moment(); console.log(`Now: ${now.format('ll')}`); now.add('3', 'days'); console.log(`Adding three days: ${now.format('ll')}`); now.subtract('2', 'years'); console.log(`Subtracting 2 years: ${now.format('ll')}`); ``` 在示例中,我們將三天相加減去兩年。 ```js now.add('3', 'days'); ... now.subtract('2', 'years'); ``` `add()`和`subtract()`方法的第二個參數是單位類型。 ```js $ node add_sub.js Now: Jul 1, 2018 Adding three days: Jul 4, 2018 Subtracting 2 years: Jul 4, 2016 ``` 這是輸出。 ## Moment.js 日期時間部分 在下面的示例中,我們獲取了當前日期時間的部分。 `parts.js` ```js const moment = require('moment'); let now = moment(); let year = now.get('year'); let month = now.get('month'); // 0 to 11 let date = now.get('date'); let hour = now.get('hour'); let minute = now.get('minute'); let second = now.get('second'); let millisecond = now.get('millisecond'); console.log("Year: " + year); console.log("Month: " + month); console.log("Date: " + date); console.log("Hour: " + hour); console.log("Minute: " + minute); console.log("Second: " + second); console.log("Millisecond: " + millisecond); ``` 該示例計算當前日期時間。 我們獲得日期時間的年,月,日,時,分,秒和毫秒部分。 ```js $ node parts.js Year: 2018 Month: 6 Date: 2 Hour: 18 Minute: 10 Second: 3 Millisecond: 329 ``` 這是一個示例輸出。 ## Moment.js 星期,月,年 下面的示例計算星期幾,月份和年份。 `dayof.js` ```js const moment = require('moment'); let now = moment(); console.log("Day of week: " + now.weekday()); console.log("Day of month: " + now.date()); console.log("Day of year: " + now.dayOfYear()); ``` `weekday()`返回星期幾,`date()`返回星期幾,`dayOfYear()`返回一年中的日子。 ```js $ node main.js Day of week: 1 Day of month: 2 Day of year: 183 ``` 這是一個示例輸出。 ## Moment.js 一年中的第幾周,季度中的第幾周 在下面的示例中,我們獲得一年中的星期,一年中的季度以及一年中的星期數。 `weeks_quarter.js` ```js const moment = require('moment'); let now = moment(); console.log("Week of year: " + now.week()); console.log("Quarter of year: " + now.quarter()); console.log("Weeks in year: " + now.weeksInYear()); ``` `week()`方法返回一年中的星期,`quarter()`方法返回一年中的季度,`weeksInYear()`方法返回一年中的星期數。 ```js $ node weeks_quarter.js Week of year: 27 Quarter of year: 3 Weeks in year: 52 ``` 這是一個示例輸出。 ## Moment.js 相對日期時間 我們可以使用`fromNow()`,`startOf()`和`endOf()`函數計算相對日期時間。 `relative_time.js` ```js const moment = require('moment'); let day = moment().startOf('year'); let now = moment(); let days = now.diff(day, 'days'); console.log(`${days} have passed since the start of the year.`); let val = moment().endOf('day'); let mins = val.diff(now, 'minutes'); console.log(`The day will end in ${mins} minutes.`); let day2 = moment("2028-12-20") let diff = day2.fromNow(); console.log(`The day will come ${diff}.`); ``` 該示例使用上述功能。 ```js let day = moment().startOf('year'); let now = moment(); let days = now.diff(day, 'days'); ``` 在這里,我們計算自年初以來經過的天數。 ```js let val = moment().endOf('day'); let mins = val.diff(now, 'minutes'); ``` 這些行計算到午夜的分鐘數。 ```js let day2 = moment("2028-12-20") let diff = day2.fromNow(); ``` 在這里,我們獲得到指定日期為止的年數。 ```js $ node relative_time.js 182 have passed since the start of the year. The day will end in 360 minutes. The day will come in 10 years. ``` 這是輸出。 ## Moment.js 檢查有效性 我們可以使用`isValid()`方法檢查日期和時間對象是否有效。 `validity.js` ```js const moment = require('moment'); let day1 = moment('2018-12-12'); let day2 = moment('2018-13-12'); if (day1.isValid()) { console.log("Day is valid"); } else { console.log("Day is not valid"); } if (day2.isValid()) { console.log("Day is valid"); } else { console.log("Day is not valid"); } ``` 該示例檢查兩天的有效性。 ## Moment.js 日期查詢 `isBefore()`和`isAfter()`函數可用于確定某個日期是在另一個日期之前還是之后。 `date_queries.js` ```js const moment = require('moment'); let d1 = moment("2018-05-19"); let d2 = moment("2018-05-20"); let d3 = moment("2018-05-22"); if (d1.isAfter(d2)) { console.log(`${d1.format('ll')} is after ${d2.format('ll')}`); } else { console.log(`${d1.format('ll')} is before ${d2.format('ll')}`); } if (d2.isBefore(d3)) { console.log(`${d2.format('ll')} is before ${d3.format('ll')}`); } else { console.log(`${d2.format('ll')} is after ${d3.format('ll')}`); } ``` 在示例中,我們使用`isBefore()`和`isAfter()`函數比較三個日期。 ```js $ node date_queries.js May 19, 2018 is before May 20, 2018 May 20, 2018 is before May 22, 2018 ``` 這是輸出。 `isBetween()`函數檢查日期是否在給定的日期范圍內。 `between.js` ```js const moment = require('moment'); let d1 = moment("2018-05-19"); if (d1.isBetween('2018-05-10', '2018-05-25')) { console.log("The day is within the date range"); } ``` 該示例使用`isBetween()`函數來確定日期是否在指定的日期范圍內。 ## Moment.js Unix 時間 Unix 時間是自 Unix 時代以來的秒數。 `unix()`函數返回自世界標準時間 1970 年 1 月 1 日 0 小時 0 分 0 秒以來的秒數。 `unixtime.js` ```js const moment = require('moment'); let unixTime = moment().unix(); console.log(unixTime); let unixTime2 = moment(1000); console.log(unixTime2.format('MM-DD-YYYY')); ``` 在該示例中,我們獲得了當前的 unix 時間并將 unix 時間 1s 轉換為人類可讀的格式。 ```js let unixTime = moment().unix(); ``` 我們通過`unix()`函數獲得 Unix 時間。 返回的值是從 Unix 紀元開始起經過的秒數。 ```js let unixTime2 = moment(1000); console.log(unixTime2.format('MM-DD-YYYY')); ``` 我們得到 1 秒的 unix 時間,并以給定的格式輸出。 ```js $ node unixtime.js 1530606134 01-01-1970 ``` 這是輸出。 ## Moment.js 解析日期和時間 通過將日期和時間格式傳遞給`moment()`函數,我們可以解析日期和時間的字符串表示形式。 `parse.js` ```js const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console.log(parsed.format('ll')); ``` 在該示例中,我們解析了一個非標準的日期和時間字符串。 我們將期望的格式作為`moment()`函數的第二個參數傳遞。 ## Moment.js 本地化的日期和時間 使用`locale()`函數,我們可以設置獲取輸出的語言環境。 `localized.js` ```js const moment = require('moment'); moment.locale('sk'); let now = moment(); console.log(now.format('LLLL')); moment.locale('de'); now = moment(); console.log(now.format('LLLL')); moment.locale('hu'); now = moment(); console.log(now.format('LLLL')); ``` 在示例中,我們在三個不同的語言環境中打印當前時刻。 ```js $ node localized.js nede?a 1\. júl 2018 22:21 Sonntag, 1\. Juli 2018 22:21 2018\. július 1., vasárnap 22:21 ``` 我們提供了當前時刻的斯洛伐克,德國和匈牙利日期和時間輸出。 ## 世界時間 我們的星球是一個球體; 它繞其軸旋轉。 地球向東旋轉,因此太陽在不同位置的不同時間升起。 地球大約每 24 小時旋轉一次。 因此,世界被劃分為 24 個時區。 在每個時區,都有一個不同的本地時間。 夏令時通常會進一步修改此本地時間。 實際需要一個全球時間。 全球時間可以避免時區和夏令時的混淆。 UTC(世界標準時間)被選為主要時間標準。 UTC 用于航空,天氣預報,飛行計劃,空中交通管制通關和地圖。 與當地時間不同,UTC 不會隨季節變化而變化。 `utc.js` ```js const moment = require('moment'); let localTime = moment(); console.log(localTime.format()); let utcTime = moment().utc(); console.log(utcTime.format('lll')); ``` 該示例打印當前的 UTC 時間和本地時間。 ```js let utcTime = moment().utc(); ``` 通用時間通過`utc()`檢索。 ```js $ node utc.js 2018-07-01T21:15:17+02:00 Jul 1, 2018 7:15 PM ``` 在我們的案例中,本地時間與世界時間之間的時差為兩個小時。 時區一個小時,夏時制另一個小時。 ## Moment.js 閏年 閏年是包含另一天的一年。 日歷中額外一天的原因是天文日歷年與日歷年之間的差異。 `leap_year.js` ```js const moment = require('moment'); // Assume year >= 1582 in the Gregorian calendar. let years = [ 2000, 2002, 2004, 2008, 2012, 2016, 2020, 1900, 1800, 1600 ]; for (year of years) { let ym = moment([year]); if (ym.isLeapYear()) { console.log(`${year} is a leap year`); } else { console.log(`${year} is not a leap year`); } } ``` 在示例中,我們有很多年。 我們確定哪些年份是閏年。 ```js if (ym.isLeapYear()) { ``` 我們使用`isLeapYear()`函數確定年份是否為閏年。 ```js $ node leap_year.js 2000 is a leap year 2002 is not a leap year 2004 is a leap year 2008 is a leap year 2012 is a leap year 2016 is a leap year 2020 is a leap year 1900 is not a leap year 1800 is not a leap year 1600 is a leap year ``` 這是輸出。 在本教程中,我們使用`Moment.js`庫在 JavaScript 中使用日期和時間。 您可能也對以下相關教程感興趣: [Day.js 教程](/javascript/dayjs/),或列出[所有 JavaScript 教程](/all/#js)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看