<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國際加速解決方案。 廣告
                # Day.js 教程 > 原文: [http://zetcode.com/javascript/dayjs/](http://zetcode.com/javascript/dayjs/) Day.js 教程展示了如何通過 Day.js 模塊在 JavaScript 中使用日期和時間。 ## Day.js Day.js 是用于解析,驗證,操作和格式化日期的極簡 JavaScript 日期庫。 它是 Moment.js 的替代庫,并且具有兼容的 API。 Moment.js 在 [Moment.js 教程](/javascript/momentjs/)中進行了介紹。 在本教程中,我們在 Node 應用中使用 Day.js。 ## 安裝 Day.js 首先,我們安裝 Day.js。 ```js $ node -v v11.5.0 ``` 我們使用 Node 版本 11.5.0。 ```js $ npm init -y ``` 我們啟動一個新的 Node 應用。 ```js $ npm i dayjs ``` 我們使用`npm i dayjs` 命令安裝 Day.js。 ## Day.js 今天的日期 在第一個示例中,我們使用 Day.js 獲取今天的日期。 `now.js` ```js const dayjs = require('dayjs'); let now = dayjs(); console.log(now.format()); ``` 該示例顯示當前日期時間。 ```js const dayjs = require('dayjs'); ``` 我們加載 Day.js 庫。 ```js let now = dayjs(); ``` 我們使用`dayjs()`獲得當前本地日期時間對象。 ```js console.log(now.format()); ``` 我們使用`format()`格式化輸出。 默認情況下,我們使用長日期時間格式。 ```js $ node now.js 2019-05-14T10:12:54+02:00 ``` 輸出為 ISO 標準格式。 日期時間部分用 T 字符分隔。 該字符串以時區結尾。 ## 創建 Day.js 對象 我們可以使用幾種方法來創建日期時間 Day.js 對象。 這些對象被格式化為人類可讀的輸出。 `create_objects.js` ```js const dayjs = require('dayjs'); let d1 = dayjs("2018-06-03"); console.log(d1.format()); let d2 = dayjs.unix(1530471537); console.log(d2.format()); let d3 = dayjs(new Date(2011, 11, 22)); console.log(d3.format()); ``` 該示例以三種不同方式創建日期和時間對象。 ```js let d1 = dayjs("2018-06-03"); ``` 我們通過解析字符串創建一個 dayjs 對象。 ```js let d2 = dayjs.unix(1530471537); console.log(d2.format()); ``` 我們使用 unix 時間戳(以秒為單位)定義日期時間對象。 ```js let d3 = dayjs(new Date(2011, 11, 22)); console.log(d3.format()); ``` 最后,我們使用 JavaScript 內置的`Date`對象來定義`dayjs`對象。 ```js $ node create_objects.js 2018-06-03T00:00:00+02:00 2018-07-01T20:58:57+02:00 2011-12-22T00:00:00+01:00 ``` 這是輸出。 ## Day.js 格式化日期時間 Day.js 對象使用`format()`函數格式化。 `formatting.js` ```js const dayjs = require('dayjs'); let now = dayjs(); 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 D YYYY")); console.log(now.format("YYYY-MM-DD")); ``` 使用日的`format()`函數的示例格式的日期和時間。 ```js $ node formatting.js ISO 2019-05-14T10:19:18+02:00 Time 10:19:18 10:19:18 am Date Tuesday, May 14 2019 2019-05-14 ``` 這是一個示例輸出。 ## Day.js 計算日期時間差 使用`diff()`函數,我們可以計算兩個日期時間對象之間的差。 `difference.js` ```js const dayjs = require('dayjs'); const date1 = dayjs("2019-14-05"); const date2 = dayjs("2018-06-25"); let df1 = date1.diff(date2); console.log(df1); let df2 = date1.diff(date2, "month"); console.log(df2); let df3 = date1.diff(date2, "month", true); console.log(df3); let df4 = date1.diff(date2, "day"); console.log(df4); let df5 = date1.diff(date2, "week"); console.log(df5); ``` 該示例以月,日和周為單位計算兩個`dayjs`對象之間的差異。 ```js let df2 = date1.diff(date2, "month"); console.log(df2); ``` 第二個參數表明輸出將以月為單位。 ```js let df3 = date1.diff(date2, "month", true); console.log(df3); ``` 將第三個參數設置為`true`會返回浮點值,例如 7.3 個月 ```js $ node difference.js 19357200000 7 7.3 224 32 ``` 這是輸出。 Borodino 戰役是 1812 年 9 月 7 日在法國入侵俄羅斯期間的拿破侖戰爭中進行的戰斗。 `borodino.js` ```js const dayjs = require('dayjs'); let borodinoBattle = dayjs('1812-09-07'); let now = dayjs(); let days = now.diff(borodinoBattle, 'days'); console.log(`On ${now.format('YYYY-MM-DD')}, ${days} days have passed since the Borodino battle.`); ``` 在示例中,我們計算了自著名戰斗以來經過的天數。 ```js $ node borodino.js On 2019-05-14, 75489 days have passed since the Borodino battle. ``` 這是一個示例輸出。 ## Day.js 日期時間算法 `add()`函數用于將日期和時間添加到`dayjs`對象,`subtract()`函數用于從`dayjs`對象中減去日期和時間。 `arithm.js` ```js const dayjs = require('dayjs'); let now = dayjs(); console.log(now.format('YYYY-MM-DD')); let d1 = now.add('14', 'day'); console.log(d1.format('YYYY-MM-DD')); let d2 = now.subtract('3', 'year'); console.log(d2.format('YYYY-MM-DD')); ``` 在示例中,我們將十四天加上當前日期時間,再減去兩年。 ```js let d1 = now.add('14', 'day'); ... let d2 = now.subtract('3', 'year');); ``` `add()`和`subtract()`函數的第二個參數是單位類型。 ```js $ node arithm.js 2019-05-14 2019-05-28 2016-05-14 ``` 這是輸出。 ## Day.js 日期時間部分 在下面的示例中,我們獲取了當前日期時間的部分。 `parts.js` ```js const dayjs = require('dayjs'); let now = dayjs(); console.log(now.toObject()); let year = now.year(); console.log(`Year: ${year}`); let month = now.month(); console.log(`Month: ${month}`); let date = now.date(); console.log(`Date: ${date}`); let hour = now.hour(); console.log(`Hour: ${hour}`); let minute = now.minute(); console.log(`Minute: ${minute}`); let second = now.second(); console.log(`Second: ${second}`); let milli = now.millisecond(); console.log(`Millisecond: ${milli}`); ``` 該示例計算當前日期時間。 我們獲得日期時間的年,月,日,時,分,秒和毫秒部分。 `toObject()`函數返回帶有日期時間部分的 JavaScript 對象。 ```js $ node parts.js { years: 2019, months: 4, date: 14, hours: 10, minutes: 41, seconds: 34, milliseconds: 963 } Year: 2019 Month: 4 Date: 14 Hour: 10 Minute: 41 Second: 34 Millisecond: 963 ``` 這是一個示例輸出。 ## Day.js 轉換函數 除了`format()`函數外,我們還可以使用一些內置的轉換函數。 `converting.js` ```js const dayjs = require('dayjs'); let now = dayjs(); console.log(now.toString()); console.log(now.toJSON()); console.log(now.toISOString()); console.log(now.toObject()); ``` 我們有四個函數。 `toJSON()`是`toISOString()`的別名。 ```js $ node converting.js Tue, 14 May 2019 09:00:51 GMT 2019-05-14T09:00:51.157Z 2019-05-14T09:00:51.157Z { years: 2019, months: 4, date: 14, hours: 11, minutes: 0, seconds: 51, milliseconds: 157 } ``` 這是輸出。 ## Day.js 相對日期時間 我們可以使用`startOf()`和`endOf()`函數計算相對日期時間。 `relative_time.js` ```js const dayjs = require('dayjs'); // let now = dayjs(); let startWeek = dayjs().startOf('week'); console.log(startWeek.format()); let endWeek = dayjs().endOf('week'); console.log(endWeek.format()); let startMonth = dayjs().startOf('month'); console.log(startMonth.format()); let endMonth = dayjs().endOf('month'); console.log(endMonth.format()); let startYear = dayjs().startOf('year'); console.log(startYear.format()); let endYear = dayjs().endOf('year'); console.log(endYear.format()); ``` 該示例使用上述函數。 ```js let startWeek = dayjs().startOf('week'); console.log(startWeek.format()); ``` 在這里,我們計算當前星期開始的日期時間。 ```js let endYear = dayjs().endOf('year'); console.log(endYear.format()); ``` 在這里,我們獲取一年中的最后一個日期時間。 ```js $ node relative_time.js 2019-05-12T00:00:00+02:00 2019-05-18T23:59:59+02:00 2019-05-01T00:00:00+02:00 2019-05-31T23:59:59+02:00 2019-01-01T00:00:00+01:00 2019-12-31T23:59:59+01:00 ``` 這是輸出。 ## Day.js 檢查有效性 我們可以使用`isValid()`函數檢查日期和時間對象是否有效。 `validating.js` ```js const dayjs = require('dayjs'); let day1 = dayjs('2018-12-12'); let day2 = dayjs('2018-11-ks'); if (day1.isValid()) { console.log("Day is valid"); console.log(day1.format()); } else { console.log("Day is not valid"); } if (day2.isValid()) { console.log("Day is valid"); console.log(day2.format()); } else { console.log("Day is not valid"); } ``` 該示例檢查兩天的有效性。 ## Day.js 日期查詢 `isSame()`,`isBefore()`和`isAfter()`函數可用于確定某個日期是在另一個日期之前還是之后。 `queries.js` ```js const dayjs = require('dayjs'); let d1 = dayjs("2018-05-19"); let d2 = dayjs("2018-05-20"); let d3 = dayjs("2018-05-22"); let d4 = dayjs("2018-05-19"); if (d1.isSame(d4)) { console.log('these are same dates'); } else { console.log('these are not the same dates'); } if (d1.isAfter(d2)) { console.log(`${d1.format('YYYY-MM-DD')} is after ${d2.format('YYYY-MM-DD')}`); } else { console.log(`${d1.format('YYYY-MM-DD')} is before ${d2.format('YYYY-MM-DD')}`); } if (d2.isBefore(d3)) { console.log(`${d2.format('YYYY-MM-DD')} is before ${d3.format('YYYY-MM-DD')}`); } else { console.log(`${d2.format('YYYY-MM-DD')} is after ${d3.format('YYYY-MM-DD')}`); } ``` 在示例中,我們比較了三個日期。 ```js $ node queries.js these are same dates 2018-05-19 is before 2018-05-20 2018-05-20 is before 2018-05-22 ``` 這是輸出。 `isBetween()`函數檢查日期是否在給定的日期范圍內。 `between.js` ```js const dayjs = require('dayjs'); const isBetween = require('dayjs/plugin/isBetween'); dayjs.extend(isBetween); let d1 = dayjs("2018-05-19"); if (d1.isBetween('2018-05-10', '2018-05-25')) { console.log("The day is within the date range"); } else { console.log("The day is not within the date range"); } ``` 該示例使用`isBetween()`函數來確定日期是否在指定的日期范圍內。 對于此示例,我們需要`isBetween`插件。 ## Day.js Unix 時間 Unix 時間是自 Unix 時代以來的秒數。 `unix()`函數返回自世界標準時間 1970 年 1 月 1 日 0 小時 0 分 0 秒以來的秒數。 `unixtime.js` ```js const dayjs = require('dayjs'); let unixTime_s = dayjs().unix(); console.log(unixTime_s); let unixTime_ms = dayjs().valueOf(); console.log(unixTime_ms); let unixTime2 = dayjs(1); console.log(unixTime2.format('YYYY-DD-MM')); ``` 在該示例中,我們獲得了當前的 unix 時間并將 unix 時間 1 s 轉換為人類可讀的格式。 ```js let unixTime_s = dayjs().unix(); ``` 我們通過`unix()`函數獲得 Unix 時間。 返回的值是從 Unix 紀元開始起經過的秒數。 ```js let unixTime_ms = dayjs().valueOf(); ``` 使用`valueOf()`函數,我們可以得到以毫秒為單位的 Unix 時間。 ```js let unixTime2 = dayjs(1); console.log(unixTime2.format('YYYY-DD-MM')); ``` 我們得到 1 秒的 unix 時間,并以給定的格式輸出。 ```js $ node unix_time.js 1557825803 1557825803834 1970-01-01 ``` 這是一個示例輸出。 ## Day.js 閏年 閏年是包含另一天的一年。 日歷中額外一天的原因是天文日歷年與日歷年之間的差異。 我們需要添加`isLeapYear`插件。 `leap_year.js` ```js const dayjs = require('dayjs'); const isLeapYear = require('dayjs/plugin/isLeapYear'); dayjs.extend(isLeapYear) // 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 = dayjs([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 ``` 這是輸出。 在本教程中,我們使用`Day.js`庫在 JavaScript 中使用日期和時間。 您可能也對以下相關教程感興趣: [Moment.js 教程](/javascript/momentjs/),或列出[所有 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>

                              哎呀哎呀视频在线观看