Moment.js เป็นห้องสมุดเวลาและวันที่ที่ยอดเยี่ยมพร้อมคุณสมบัติและสาธารณูปโภคมากมาย อย่างไรก็ตามหากคุณกำลังทำงานกับแอปพลิเคชันเว็บที่อ่อนไหวต่อประสิทธิภาพมันอาจทำให้เกิดค่าใช้จ่ายจำนวนมากเนื่องจาก API ที่ซับซ้อนและขนาดใหญ่

ปัญหาเกี่ยวกับ moment.js:
a.subtract('ms', 50) , A.Subtract a.subtract(50, 'ms') a.subtract('s', '50')หากคุณไม่ได้ใช้เขตเวลา แต่มีฟังก์ชั่นง่ายๆเพียงไม่กี่อย่างจากช่วงเวลาที่ js สิ่งนี้อาจขยายแอพของคุณและดังนั้นจึงถือว่าเป็น overkill DayJS มีแกนกลางที่เล็กกว่าและมี API ที่คล้ายกันมากจึงทำให้ง่ายต่อการโยกย้าย Date-fns ช่วยให้การเขย่าต้นไม้และผลประโยชน์อื่น ๆ เพื่อให้ได้ผลดีกับ React, Sinon.js และ Webpack ฯลฯ ดูช่วงเวลา/ช่วงเวลา#2373 สำหรับแนวคิดเพิ่มเติมว่าทำไมและวิธีการที่ผู้คนเปลี่ยนจากช่วงเวลา. js เป็นโซลูชั่นอื่น ๆ
| ชื่อ | การเขย่าต้นไม้ | วิธีการที่ร่ำรวย | ลวดลาย | สถานที่ | การสนับสนุนเขตเวลา | ความนิยม (ดาว) | ขนาด |
|---|---|---|---|---|---|---|---|
| moment.js | เลขที่ | สูง | โอ | 123 | ดี (ช่วงเวลาช่วงเวลา) | ||
| Luxon | เลขที่ | สูง | โอ | - | ดี (INTL) | ||
| วันที่ | ใช่ | สูง | เกี่ยวกับการทำงาน | 64 | ดี (date-fns-tz) | ||
| dayjs | เลขที่ | สูง | โอ | 138 | ดี (INTL) |
ลบ moment.js เพื่อแทนที่ด้วย date -fns - สร้างเอาต์พุตลดลง 40%
—Jared Farago จากโครงการ WebNode
ห้องสมุดที่ดีหากคุณต้องการแทนที่ช่วงเวลาที่ js ด้วยเหตุผลใดก็ตาม ไม่เปลี่ยนรูปด้วย
—Dan Abramov ผู้แต่ง Redux และผู้เขียนร่วมของ Create React App การสร้างเครื่องมือสำหรับมนุษย์
ฉันขอแนะนำอย่างยิ่งให้ใช้ date-fns ในช่วงเวลาที่ js มี API ที่ดีกว่าและคุณสามารถรวมเฉพาะส่วนที่คุณต้องการ!
—Matija Marohnićนักพัฒนาส่วนหน้าการออกแบบที่มีความชำนาญจากโครเอเชีย
หากคุณใช้ ESLINT คุณสามารถติดตั้งปลั๊กอินที่จะช่วยคุณระบุสถานที่ใน codebase ของคุณที่คุณไม่ต้องการ (อาจไม่) moment.js
ติดตั้งปลั๊กอิน ...
npm install --save-dev eslint-plugin-you-dont-need-momentjs... จากนั้นอัปเดตการกำหนดค่าของคุณ
"extends" : [ "plugin:you-dont-need-momentjs/recommended" ] , แยกวิเคราะห์
รับ + set
จัดการ
แสดง
สอบถาม
| พื้นเมือง | Luxon | วันที่ | dayjs | เกี่ยวกับชั่วคราว | |
|---|---|---|---|---|---|
| แยกวิเคราะห์ | |||||
| รูปแบบสตริง + วันที่ | |||||
| รูปแบบสตริง + เวลา | |||||
| สตริง + ฟอร์แมต + ตำแหน่ง | |||||
| รับ + set | |||||
| มิลลิวินาที/วินาที/นาที/ชั่วโมง | |||||
| วันที่เดือน | |||||
| วันของสัปดาห์ | |||||
| วันของปี | |||||
| สัปดาห์ของปี | |||||
| วันในเดือน | |||||
| สัปดาห์ในปี | |||||
| สูงสุดของวันที่ที่กำหนด | |||||
| ขั้นต่ำของวันที่กำหนด | |||||
| จัดการ | |||||
| เพิ่ม | |||||
| ลบออก | |||||
| เริ่มเวลา | |||||
| สิ้นเวลา | |||||
| แสดง | |||||
| รูปแบบ | |||||
| เวลาจากนี้ | |||||
| เวลาจาก x | |||||
| ความแตกต่าง | |||||
| สอบถาม | |||||
| ก่อนหน้านี้ | |||||
| เหมือนกัน | |||||
| หลังจากนั้น | |||||
| อยู่ระหว่าง | |||||
| เป็นปีที่ก้าวกระโดด | |||||
| เป็นวันที่ |
ส่งคืนวันที่แยกวิเคราะห์จากสตริงวันที่โดยใช้สตริงรูปแบบที่กำหนด
// Moment.js
moment ( '12-25-1995' , 'MM-DD-YYYY' ) ;
// => "1995-12-24T13:00:00.000Z"
// Native
const datePattern = / ^(d{2})-(d{2})-(d{4})$ / ;
const [ , month , day , year ] = datePattern . exec ( '12-25-1995' ) ;
new Date ( ` ${ month } , ${ day } ${ year } ` ) ;
// => "1995-12-24T13:00:00.000Z"
// date-fns
import parse from 'date-fns/parse' ;
parse ( '12-25-1995' , 'MM-dd-yyyy' , new Date ( ) ) ;
// => "1995-12-24T13:00:00.000Z"
// dayjs
dayjs ( '12-25-1995' ) ;
// => "1995-12-24T13:00:00.000Z"
// luxon
DateTime . fromFormat ( '12-25-1995' , 'MM-dd-yyyy' ) . toJSDate ( ) ;
// => "1995-12-24T13:00:00.000Z"
// Temporal
const datePattern = / ^(d{2})-(d{2})-(d{4})$ / ;
const [ , month , day , year ] = datePattern . exec ( '12-25-1995' ) ;
new Temporal . ZonedDateTime . from ( { year , month , day , timeZone : Temporal . Now . timeZone ( ) } ) ;
// => "1995-12-24T13:00:00.000Z"⬆กลับไปด้านบน
ส่งคืนวันที่แยกวิเคราะห์จากสตริงเวลาโดยใช้สตริงรูปแบบที่กำหนด
// Moment.js
moment ( '2010-10-20 4:30' , 'YYYY-MM-DD HH:mm' ) ;
// => "2010-10-19T17:30:00.000Z"
// Native
const datePattern = / ^(d{4})-(d{2})-(d{2})s(d{1,2}):(d{2})$ / ;
const [ , year , month , day , rawHour , min ] = datePattern . exec ( '2010-10-20 4:30' ) ;
new Date ( ` ${ year } - ${ month } - ${ day } T ${ ( '0' + rawHour ) . slice ( - 2 ) } : ${ min } :00` ) ;
// => "2010-10-19T17:30:00.000Z"
// date-fns
import parse from 'date-fns/parse' ;
parse ( '2010-10-20 4:30' , 'yyyy-MM-dd H:mm' , new Date ( ) ) ;
// => "2010-10-19T17:30:00.000Z"
// dayjs ️ requires customParseFormat plugin
import customParseFormat from 'dayjs/plugin/customParseFormat' ;
dayjs . extend ( customParseFormat ) ;
dayjs ( '2010-10-20 4:30' , 'YYYY-MM-DD HH:mm' ) ;
// => "2010-10-19T17:30:00.000Z"
// luxon
DateTime . fromFormat ( '2010-10-20 4:30' , 'yyyy-MM-dd H:mm' ) . toJSDate ( ) ;
// => "2010-10-19T17:30:00.000Z"
// Temporal
const datePattern = / ^(d{4})-(d{2})-(d{2})s(d{1,2}):(d{2})$ / ;
const [ , year , month , day , hour , minute ] = datePattern . exec ( '2010-10-20 4:30' ) ;
new Temporal . ZonedDateTime . from ( { year , month , day , hour , minute , timeZone : Temporal . Now . timeZone ( ) } ) ;
// => "2010-10-19T17:30:00.000Z"⬆กลับไปด้านบน
ส่งคืนวันที่แยกวิเคราะห์จากสตริงโดยใช้สตริงรูปแบบและสถานที่ที่กำหนด
// Moment.js
moment ( '2012 mars' , 'YYYY MMM' , 'fr' ) ;
// => "2012-02-29T13:00:00.000Z"
// date-fns
import parse from 'date-fns/parse' ;
import fr from 'date-fns/locale/fr' ;
parse ( '2012 mars' , 'yyyy MMMM' , new Date ( ) , { locale : fr } ) ;
// => "2012-02-29T13:00:00.000Z"
// dayjs ️ requires customParseFormat plugin
import customParseFormat from 'dayjs/plugin/customParseFormat' ;
import 'dayjs/locale/fr' ;
dayjs . extend ( customParseFormat ) ;
dayjs ( '2012 mars' , 'YYYY MMM' , 'fr' ) ;
// => "2012-02-29T13:00:00.000Z"
// Luxon does not support Locale for node unless https://moment.github.io/luxon/docs/manual/install.html#node
DateTime . fromFormat ( '2012 mars' , 'yyyy MMMM' , { locale : 'fr' } ) ;
// => "2012-02-29T13:00:00.000Z"⬆กลับไปด้านบน
รับ Millisecond/Second/Minute/Hour ของวันที่ที่กำหนด
// Moment.js
moment ( ) . seconds ( ) ;
// => 49
moment ( ) . hours ( ) ;
// => 19
// Native
new Date ( ) . getSeconds ( ) ;
// => 49
new Date ( ) . getHours ( ) ;
// => 19
// date-fns
import getSeconds from 'date-fns/getSeconds' ;
import getHours from 'date-fns/getHours' ;
getSeconds ( new Date ( ) ) ;
// => 49
getHours ( new Date ( ) ) ;
// => 19
// dayjs
dayjs ( ) . second ( ) ;
// => 49
dayjs ( ) . hour ( ) ;
// => 19
// Luxon
DateTime . local ( ) . second ;
// => 49
DateTime . local ( ) . hour ;
// => 19
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . second ;
// => 49
Temporal . Now . zonedDateTimeISO ( ) . hour ;
// => 19| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1500.703ms |
| พื้นเมือง | 348.411ms |
| วันที่ | 520.670ms |
| dayjs | 494.234ms |
| Luxon | 1208.368ms |
| เกี่ยวกับชั่วคราว | - |
ตั้งค่า Millisecond/Second/Minute/Hour ของวันที่ที่กำหนด
// Moment.js
moment ( ) . seconds ( 30 ) ;
// => "2018-09-09T09:12:30.695Z"
moment ( ) . hours ( 13 ) ;
// => "2018-09-09T03:12:49.695Z"
// Native
new Date ( new Date ( ) . setSeconds ( 30 ) ) ;
// => "2018-09-09T09:12:30.695Z"
new Date ( new Date ( ) . setHours ( 13 ) ) ;
// => "2018-09-09T03:12:49.695Z"
// date-fns
import setSeconds from 'date-fns/setSeconds' ;
import setHours from 'date-fns/setHours' ;
setSeconds ( new Date ( ) , 30 ) ;
// => "2018-09-09T09:12:30.695Z"
setHours ( new Date ( ) , 13 ) ;
// => "2018-09-09T03:12:49.695Z"
// dayjs
dayjs ( ) . set ( 'second' , 30 ) ;
// => "2018-09-09T09:12:30.695Z"
dayjs ( ) . set ( 'hour' , 13 ) ;
// => "2018-09-09T03:12:49.695Z"
// luxon
DateTime . utc ( )
. set ( { second : 30 } )
. toJSDate ( ) ;
// => "2018-09-09T09:12:30.695Z"
DateTime . utc ( )
. set ( { hour : 13 } )
. toJSDate ( ) ;
// => "2018-09-09T03:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . with ( { second : 30 } ) ;
// => "2018-09-09T09:12:30.695Z"
Temporal . Now . zonedDateTimeISO ( ) . with ( { hour : 13 } ) ;
// => "2018-09-09T03:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1689.744ms |
| พื้นเมือง | 636.741ms |
| วันที่ | 714.148ms |
| dayjs | 2037.603ms |
| Luxon | 2897.571ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
รับหรือตั้งค่าวันของเดือน
// Moment.js
moment ( ) . date ( ) ;
// => 9
moment ( ) . date ( 4 ) ;
// => "2018-09-04T09:12:49.695Z"
// Native
new Date ( ) . getDate ( ) ;
// => 9
new Date ( ) . setDate ( 4 ) ;
// => "2018-09-04T09:12:49.695Z"
// date-fns
import getDate from 'date-fns/getDate' ;
import setDate from 'date-fns/setDate' ;
getDate ( new Date ( ) ) ;
// => 9
setDate ( new Date ( ) , 4 ) ;
// => "2018-09-04T09:12:49.695Z"
// dayjs
dayjs ( ) . date ( ) ;
// => 9
dayjs ( ) . set ( 'date' , 4 ) ;
// => "2018-09-04T09:12:49.695Z"
// luxon
DateTime . utc ( ) . day ;
// => 9
DateTime . utc ( )
. set ( { day : 4 } )
. toString ( ) ;
// => "2018-09-04T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . day ;
// => 9
Temporal . Now . zonedDateTimeISO ( ) . with ( { day : 4 } ) ;
// => "2018-09-04T09:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1381.669ms |
| พื้นเมือง | 397.415ms |
| วันที่ | 588.004ms |
| dayjs | 1218.025ms |
| Luxon | 2705.606ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
รับหรือตั้งค่าวันของสัปดาห์
// Moment.js
moment ( ) . day ( ) ;
// => 0 (Sunday)
moment ( ) . day ( - 14 ) ;
// => "2018-08-26T09:12:49.695Z"
// Native
new Date ( ) . getDay ( ) ;
// => 0 (Sunday)
new Date ( ) . setDate ( new Date ( ) . getDate ( ) - 14 ) ;
// => "2018-08-26T09:12:49.695Z"
// date-fns
import getDay from 'date-fns/getDay' ;
import setDay from 'date-fns/setDay' ;
getDay ( new Date ( ) ) ;
// => 0 (Sunday)
setDay ( new Date ( ) , - 14 ) ;
// => "2018-08-26T09:12:49.695Z"
// dayjs
dayjs ( ) . day ( ) ;
// => 0 (Sunday)
dayjs ( ) . set ( 'day' , - 14 ) ;
// => "2018-08-26T09:12:49.695Z"
// Luxon
DateTime . local ( ) . weekday ;
// => 7 (Sunday)
DateTime . local ( )
. minus ( { day : 14 } )
. toJSDate ( ) ;
// => "2018-08-26T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . dayOfWeek ;
// => 7 (Sunday)
Temporal . Now . zonedDateTimeISO ( ) . subtract ( Temporal . Duration . from ( { days : 14 } ) ) ;
// => "2018-09-04T09:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1919.404ms |
| พื้นเมือง | 543.466ms |
| วันที่ | 841.436ms |
| dayjs | 1229.475ms |
| Luxon | 3936.282ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ได้รับหรือตั้งค่าวันของปี
// Moment.js
moment ( ) . dayOfYear ( ) ;
// => 252
moment ( ) . dayOfYear ( 256 ) ;
// => "2018-09-13T09:12:49.695Z"
// Native
Math . floor (
( new Date ( ) - new Date ( new Date ( ) . getFullYear ( ) , 0 , 0 ) ) / 1000 / 60 / 60 / 24
) ;
// => 252
// date-fns
import getDayOfYear from 'date-fns/getDayOfYear' ;
import setDayOfYear from 'date-fns/setDayOfYear' ;
getDayOfYear ( new Date ( ) ) ;
// => 252
setDayOfYear ( new Date ( ) , 256 ) ;
// => "2018-09-13T09:12:49.695Z"
// dayjs ️ requires dayOfYear plugin
import dayOfYear from 'dayjs/plugin/dayOfYear' ;
dayjs . extend ( dayOfYear ) ;
dayjs ( ) . dayOfYear ( ) ;
// => 252
dayjs ( ) . dayOfYear ( 256 ) ;
// => "2018-09-13T09:12:49.695Z"
// Luxon
DateTime . local ( ) . ordinal ;
// => 252
DateTime . local ( )
. set ( { ordinal : 256 } )
. toString ( ) ;
// => "2018-09-13T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . dayOfYear ;
// => 252
Temporal . Now . zonedDateTimeISO ( ) . with ( { month : 1 , day : 1 } ) . add ( Temporal . Duration . from ( { days : 256 } ) ) ;
// => "2018-09-04T09:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 5511.172ms |
| พื้นเมือง | 530.592ms |
| วันที่ | 2079.043ms |
| dayjs | - |
| Luxon | 3540.810ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ได้รับหรือตั้งสัปดาห์ของปี
// Moment.js
moment ( ) . week ( ) ;
// => 37
moment ( ) . week ( 24 ) ;
// => "2018-06-10T09:12:49.695Z"
// date-fns
import getWeek from 'date-fns/getWeek' ;
import setWeek from 'date-fns/setWeek' ;
getWeek ( new Date ( ) ) ;
// => 37
setWeek ( new Date ( ) , 24 ) ;
// => "2018-06-10T09:12:49.695Z"
// native getWeek
const day = new Date ( ) ;
const MILLISECONDS_IN_WEEK = 604800000 ;
const firstDayOfWeek = 1 ; // monday as the first day (0 = sunday)
const startOfYear = new Date ( day . getFullYear ( ) , 0 , 1 ) ;
startOfYear . setDate (
startOfYear . getDate ( ) + ( firstDayOfWeek - ( startOfYear . getDay ( ) % 7 ) )
) ;
const dayWeek = Math . round ( ( day - startOfYear ) / MILLISECONDS_IN_WEEK ) + 1 ;
// => 37
// native setWeek
const day = new Date ( ) ;
const week = 24 ;
const MILLISECONDS_IN_WEEK = 604800000 ;
const firstDayOfWeek = 1 ; // monday as the first day (0 = sunday)
const startOfYear = new Date ( day . getFullYear ( ) , 0 , 1 ) ;
startOfYear . setDate (
startOfYear . getDate ( ) + ( firstDayOfWeek - ( startOfYear . getDay ( ) % 7 ) )
) ;
const dayWeek = Math . round ( ( day - startOfYear ) / MILLISECONDS_IN_WEEK ) + 1 ;
day . setDate ( day . getDate ( ) - ( dayWeek - week ) * 7 ) ;
day . toISOString ( ) ;
// => "2018-06-10T09:12:49.794Z
// dayjs ️ requires weekOfYear plugin
import weekOfYear from 'dayjs/plugin/weekOfYear' ;
dayjs . extend ( weekOfYear ) ;
dayjs ( ) . week ( ) ;
// => 37
dayjs ( ) . week ( 24 ) ;
// => "2018-06-10T09:12:49.695Z"
// Luxon
DateTime . local ( ) . weekNumber ;
// => 37
DateTime . local ( )
. set ( { weekNumber : 23 } )
. toString ( ) ;
// => "2018-06-10T09:12:49.794Z
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . weekOfYear ;
// => 252
Temporal . Now . zonedDateTimeISO ( ) . with ( { month : 1 , day : 1 } ) . add ( Temporal . Duration . from ( { weeks : 23 } ) ) ;
// => "2018-09-04T09:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 7147.201ms |
| พื้นเมือง | 1371.631ms |
| วันที่ | 5834.815ms |
| dayjs | - |
| Luxon | 4514.771ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
รับจำนวนวันในเดือนปัจจุบัน
// Moment.js
moment ( '2012-02' , 'YYYY-MM' ) . daysInMonth ( ) ;
// => 29
// Native
new Date ( 2012 , 02 , 0 ) . getDate ( ) ;
// => 29
// date-fns
import getDaysInMonth from 'date-fns/getDaysInMonth' ;
getDaysInMonth ( new Date ( 2012 , 1 ) ) ;
// => 29
// dayjs
dayjs ( '2012-02' ) . daysInMonth ( ) ;
// => 29
// Luxon
DateTime . local ( 2012 , 2 ) . daysInMonth ;
// => 29
// Temporal
( new Temporal . PlainYearMonth ( 2012 , 2 ) ) . daysInMonth
// or
Temporal . PlainYearMonth . from ( '2012-02' ) . daysInMonth
// => 29| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 4415.065ms |
| พื้นเมือง | 186.196ms |
| วันที่ | 634.084ms |
| dayjs | 2465.774ms |
| Luxon | 1403.032ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ได้รับจำนวนสัปดาห์ในปีปัจจุบันตาม ISO สัปดาห์
// Moment.js
moment ( ) . isoWeeksInYear ( ) ;
// => 52
// Native
const year = new Date ( ) . getFullYear ( ) ;
const MILLISECONDS_IN_WEEK = 604800000 ;
const firstMondayThisYear = new Date ( + year , 0 , 5 - ( new Date ( + year , 0 , 4 ) . getDay ( ) || 7 ) ) ;
const firstMondayNextYear = new Date ( + year + 1 , 0 , 5 - ( new Date ( + year + 1 , 0 , 4 ) . getDay ( ) || 7 ) ) ;
( firstMondayNextYear - firstMondayThisYear ) / MILLISECONDS_IN_WEEK ;
// => 52
// date-fns
import getISOWeeksInYear from 'date-fns/getISOWeeksInYear' ;
getISOWeeksInYear ( new Date ( ) ) ;
// => 52
// dayjs ️ requires isoWeeksInYear plugin
import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear' ;
dayjs . extend ( isoWeeksInYear ) ;
dayjs ( ) . isoWeeksInYear ( ) ;
// => 52
// Luxon
DateTime . local ( ) . weeksInWeekYear ;
// => 52
// Temporal
Temporal . PlainDate . from ( { day : 31 , month : 12 , year : Temporal . Now . plainDateISO ( ) } ) . weekOfYear
// => 52| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1065.247ms |
| พื้นเมือง | - |
| วันที่ | 4954.042ms |
| dayjs | - |
| Luxon | 1134.483ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ส่งคืนค่าสูงสุด (อนาคตที่ห่างไกลที่สุด) ของวันที่ที่กำหนด
const array = [
new Date ( 2017 , 4 , 13 ) ,
new Date ( 2018 , 2 , 12 ) ,
new Date ( 2016 , 0 , 10 ) ,
new Date ( 2016 , 0 , 9 ) ,
] ;
// Moment.js
moment . max ( array . map ( a => moment ( a ) ) ) ;
// => "2018-03-11T13:00:00.000Z"
// Native
new Date ( Math . max . apply ( null , array ) ) . toISOString ( ) ;
// => "2018-03-11T13:00:00.000Z"
// date-fns
import max from 'date-fns/max' ;
max ( array ) ;
// => "2018-03-11T13:00:00.000Z"
// dayjs ️ requires minMax plugin
import minMax from 'dayjs/plugin/minMax' ;
dayjs . extend ( minMax ) ;
dayjs . max ( array . map ( a => dayjs ( a ) ) ) ;
// => "2018-03-11T13:00:00.000Z"
// Luxon
DateTime . max ( ... array . map ( a => DateTime . fromJSDate ( a ) ) ) . toJSDate ( ) ;
// => "2018-03-11T13:00:00.000Z"
// Temporal
Temporal . Instant . fromEpochMilliseconds ( Math . max . apply ( null , array ) )
// => "2018-03-11T13:00:00.000Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1780.075ms |
| พื้นเมือง | 828.332ms |
| วันที่ | 980.938ms |
| dayjs | - |
| Luxon | 2694.702ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ส่งคืนค่าต่ำสุด (อนาคตที่ห่างไกลที่สุด) ของวันที่ที่กำหนด
const array = [
new Date ( 2017 , 4 , 13 ) ,
new Date ( 2018 , 2 , 12 ) ,
new Date ( 2016 , 0 , 10 ) ,
new Date ( 2016 , 0 , 9 ) ,
] ;
// Moment.js
moment . min ( array . map ( a => moment ( a ) ) ) ;
// => "2016-01-08T13:00:00.000Z"
// Native
new Date ( Math . min . apply ( null , array ) ) . toISOString ( ) ;
// => "2016-01-08T13:00:00.000Z"
// date-fns
import min from 'date-fns/min' ;
min ( array ) ;
// => "2016-01-08T13:00:00.000Z"
// dayjs ️ requires minMax plugin
import minMax from 'dayjs/plugin/minMax' ;
dayjs . extend ( minMax ) ;
dayjs . min ( array . map ( a => dayjs ( a ) ) ) ;
// => "2016-01-08T13:00:00.000Z"
// Luxon
DateTime . min ( ... array . map ( a => DateTime . fromJSDate ( a ) ) ) . toJSDate ( ) ;
// => "2016-01-08T13:00:00.000Z"
// Temporal
Temporal . Instant . fromEpochMilliseconds ( Math . min . apply ( null , array ) )
// => "2018-03-11T13:00:00.000Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1744.459ms |
| พื้นเมือง | 819.646ms |
| วันที่ | 841.249ms |
| dayjs | - |
| Luxon | 2720.462ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
เพิ่มจำนวนวันที่ระบุในวันที่ที่กำหนด
// Moment.js
moment ( ) . add ( 7 , 'days' ) ;
// => "2018-09-16T09:12:49.695Z"
// Native
const now = new Date ( ) ;
now . setDate ( now . getDate ( ) + 7 ) ;
// => "Sun Sep 16 2018 09:12:49"
// date-fns
import addDays from 'date-fns/addDays' ;
addDays ( new Date ( ) , 7 ) ;
// => "2018-09-16T09:12:49.695Z"
// dayjs
dayjs ( ) . add ( 7 , 'day' ) ;
// => "2018-09-16T09:12:49.695Z"
// Luxon
DateTime . local ( )
. plus ( { day : 7 } )
. toJSDate ( ) ;
// => "2018-09-16T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . add ( Temporal . Duration . from ( { days : 7 } ) ) ;
// => "2018-09-16T09:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1309.485ms |
| พื้นเมือง | 259.932ms |
| วันที่ | 385.394ms |
| dayjs | 1911.881ms |
| Luxon | 3919.797ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ลบจำนวนวันที่ระบุจากวันที่ที่กำหนด
// Moment.js
moment ( ) . subtract ( 7 , 'days' ) ;
// => "2018-09-02T09:12:49.695Z"
// Native
const now = new Date ( ) ;
now . setDate ( now . getDate ( ) - 7 ) ;
// => Sun Sep 09 2018 09:12:49
// date-fns
import subDays from 'date-fns/subDays' ;
subDays ( new Date ( ) , 7 ) ;
// => "2018-09-02T09:12:49.695Z"
// dayjs
dayjs ( ) . subtract ( 7 , 'day' ) ;
// => "2018-09-02T09:12:49.695Z"
// Luxon
DateTime . local ( )
. minus ( { day : 7 } )
. toJSDate ( ) ;
// => "2018-09-02T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . subtract ( Temporal . Duration . from ( { days : 7 } ) ) ;
// => "2018-09-02T09:12:49.695Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1278.384ms |
| พื้นเมือง | 215.255ms |
| วันที่ | 379.057ms |
| dayjs | 1772.593ms |
| Luxon | 4028.866MS |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ส่งคืนเริ่มต้นหน่วยเวลาสำหรับวันที่ที่กำหนด
// Moment.js
moment ( ) . startOf ( 'month' ) ;
// => "2018-08-31T14:00:00.000Z"
// date-fns
import startOfMonth from 'date-fns/startOfMonth' ;
startOfMonth ( new Date ( ) ) ;
// => "2018-08-31T14:00:00.000Z"
// dayjs
dayjs ( ) . startOf ( 'month' ) ;
// => "2018-08-31T14:00:00.000Z"
// Luxon
DateTime . local ( ) . startOf ( 'month' ) ;
// => "2018-09-02T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . with ( { day : 1 } ) ;
// => "2018-09-01T14:00:00.000Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1078.948ms |
| พื้นเมือง | - |
| วันที่ | 398.107ms |
| dayjs | 765.358ms |
| Luxon | 2306.765ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
กลับจุดสิ้นสุดของหน่วยเวลาสำหรับวันที่ที่กำหนด
// Moment.js
moment ( ) . endOf ( 'day' ) ;
// => "2018-09-09T13:59:59.999Z"
// Native
const end = new Date ( ) ;
end . setHours ( 23 , 59 , 59 , 999 ) ;
end . toISOString ( ) ;
// => "2018-09-09T16:59:59.999Z"
// date-fns
import endOfDay from 'date-fns/endOfDay' ;
endOfDay ( new Date ( ) ) ;
// => "2018-09-09T13:59:59.999Z"
// dayjs
dayjs ( ) . endOf ( 'day' ) ;
// => "2018-09-09T13:59:59.999Z"
// Luxon
DateTime . local ( ) . endOf ( 'day' ) ;
// => "2018-09-02T09:12:49.695Z"
// Temporal
Temporal . Now . zonedDateTimeISO ( ) . withPlainTime ( new Temporal . PlainTime ( 23 , 59 , 59 , 999 , 999 , 999 ) ) ;
// => "2018-09-09T16:59:59.999999999Z"| ห้องสมุด | เวลา |
|---|---|
| ช่วงเวลา | 1241.304ms |
| พื้นเมือง | 225.519ms |
| วันที่ | 319.773ms |
| dayjs | 914.425ms |
| Luxon | 9920.529ms |
| เกี่ยวกับชั่วคราว | - |
⬆กลับไปด้านบน
ส่งคืนสตริงวันที่ที่จัดรูปแบบในรูปแบบที่กำหนด
// Moment.js
moment ( ) . format ( 'dddd, MMMM Do YYYY, h:mm:ss A' ) ;
// => "Sunday, September 9th 2018, 7:12:49 PM"
moment ( ) . format ( 'ddd, hA' ) ;
// => "Sun, 7PM"
// Native
new Intl . DateTimeFormat ( 'en-US' , { dateStyle : 'full' , timeStyle : 'medium' } ) . format ( new Date ( ) )
// => "Sunday, September 9, 2018 at 7:12:49 PM"
new Intl . DateTimeFormat ( 'en-US' , { weekday : 'short' , hour : 'numeric' } ) . format ( new Date ( ) )
// => "Sun, 7 PM"
// date-fns
import { intlFormat } from 'date-fns'
intlFormat ( new Date ( ) , { dateStyle : 'full' , timeStyle : 'medium' } , { locale : 'en-US' , } )
// => "Sunday, September 9, 2018 at 7:12:49 PM"
intlFormat ( new Date ( ) , { weekday : 'short' , hour : 'numeric' } , { locale : 'en-US' , } )
// => "Sun, 7 PM"
// dayjs
dayjs ( ) . format ( 'dddd, MMMM D YYYY, h:mm:ss A' ) ;
// => "Sunday, September 9 2018, 7:12:49 PM"
dayjs ( ) . format ( 'ddd, hA' ) ;
// => "Sun, 7PM"
// dayjs ️ requires advancedFormat plugin to support more format tokens
import advancedFormat from 'dayjs/plugin/advancedFormat' ;
dayjs . extend ( advancedFormat ) ;
dayjs ( ) . format ( 'dddd, MMMM Do YYYY, h:mm:ss A' ) ;
// => "Sunday, September 9th 2018, 7:12:49 PM"
// Luxon
DateTime . fromMillis ( time ) . toFormat ( 'EEEE, MMMM dd yyyy, h:mm:ss a' ) ;
// => "Sunday, September 9 2018, 7:12:49 PM" ️ not support 9th
DateTime . fromMillis ( time ) . toFormat ( 'EEE, ha' ) ;
// => "Sun, 7PM"
// Temporal
new Intl . DateTimeFormat ( 'en-US' , { dateStyle : 'full' , timeStyle : 'medium' } ) . format ( Temporal . Now . zonedDateTimeISO ( ) )
// => "Sunday, September 9, 2018 at 7:12:49 PM"
new Intl . DateTimeFormat ( 'en-US' , { weekday : 'short' , hour : 'numeric' } ) . format ( Temporal . Now . zonedDateTimeISO ( ) )
// => "Sun, 7 PM"⬆กลับไปด้านบน
เวลากลับจากนี้
// Moment.js
moment ( 1536484369695 ) . fromNow ( ) ;
// => "4 days ago"
// Native
new Intl . RelativeTimeFormat ( ) . format ( - 4 , 'day' ) ;
// => "4 days ago"
// date-fns
import formatDistance from 'date-fns/formatDistance' ;
formatDistance ( new Date ( 1536484369695 ) , new Date ( ) , { addSuffix : true } ) ;
// => "4 days ago"
// dayjs ️ requires relativeTime plugin
import relativeTime from 'dayjs/plugin/relativeTime' ;
dayjs . extend ( relativeTime ) ;
dayjs ( 1536484369695 ) . fromNow ( ) ;
// => "5 days ago" ️ the rounding method of this plugin is different from moment.js and date-fns, use with care.
// luxon requires Intl.RelativeTimeFormat
DateTime . local ( 2022 , 1 , 27 ) . toRelative ( { base : this } )
// => "in 4 months"
// Temporal
new Intl . RelativeTimeFormat ( ) . format ( - 4 , 'day' ) ;
// => "4 days ago"⬆กลับไปด้านบน
เวลากลับจาก x
// Moment.js
moment ( [ 2007 , 0 , 27 ] ) . to ( moment ( [ 2007 , 0 , 29 ] ) ) ;
// => "in 2 days"
// date-fns
import formatDistance from 'date-fns/formatDistance' ;
formatDistance ( new Date ( 2007 , 0 , 27 ) , new Date ( 2007 , 0 , 29 ) ) ;
// => "2 days"
// dayjs ️ requires relativeTime plugin
import relativeTime from 'dayjs/plugin/relativeTime' ;
dayjs . extend ( relativeTime ) ;
dayjs ( '2007-01-27' ) . to ( dayjs ( '2007-01-29' ) ) ;
// => "in 2 days"
// luxon does not support relative time
// Temporal
Temporal . PlainDate . from ( '2007-01-27' ) . until ( '2007-01-29' ) ;
// => Temporal.Duration('P2D')⬆กลับไปด้านบน
รับหน่วยเวลาระหว่างวันที่ที่กำหนด
// Moment.js
moment ( [ 2007 , 0 , 27 ] ) . diff ( moment ( [ 2007 , 0 , 29 ] ) ) ;
// => -172800000
moment ( [ 2007 , 0 , 27 ] ) . diff ( moment ( [ 2007 , 0 , 29 ] ) , 'days' ) ;
// => -2
// Native
new Date ( 2007 , 0 , 27 ) - new Date ( 2007 , 0 , 29 ) ;
// => -172800000
Math . ceil (
( new Date ( 2007 , 0 , 27 ) - new Date ( 2007 , 0 , 29 ) ) / 1000 / 60 / 60 / 24
) ;
// => -2
// date-fns
import differenceInMilliseconds from 'date-fns/differenceInMilliseconds' ;
differenceInMilliseconds ( new Date ( 2007 , 0 , 27 ) , new Date ( 2007 , 0 , 29 ) ) ;
// => -172800000
import differenceInDays from 'date-fns/differenceInDays' ;
differenceInDays ( new Date ( 2007 , 0 , 27 ) , new Date ( 2007 , 0 , 29 ) ) ;
// => -2
// dayjs
dayjs ( '2007-01-27' ) . diff ( dayjs ( '2007-01-29' ) , 'milliseconds' ) ;
// => -172800000
dayjs ( '2007-01-27' ) . diff ( dayjs ( '2007-01-29' ) , 'days' ) ;
// => -2
// luxon
DateTime . local ( 2007 , 1 , 27 ) . diff ( DateTime . local ( 2007 , 1 , 29 ) ) . milliseconds ;
// => -172800000
DateTime . local ( 2007 , 1 , 27 ) . diff ( DateTime . local ( 2007 , 1 , 29 ) , 'days' ) . days ;
// => -2
// Temporal
Temporal . PlainDate . from ( '2007-01-27' ) . since ( '2007-01-29' ) . total ( { unit : 'millisecond' } ) ;
// => -172800000
Temporal . PlainDate . from ( '2007-01-27' ) . since ( '2007-01-29' ) . total ( { unit : 'day' } ) ;
// => -2⬆กลับไปด้านบน
ตรวจสอบว่าวันที่ก่อนวันที่อื่น
// Moment.js
moment ( '2010-10-20' ) . isBefore ( '2010-10-21' ) ;
// => true
// Native
new Date ( 2010 , 10 , 20 ) < new Date ( 2010 , 10 , 21 ) ;
// => true
// date-fns
import isBefore from 'date-fns/isBefore' ;
isBefore ( new Date ( 2010 , 9 , 20 ) , new Date ( 2010 , 9 , 21 ) ) ;
// => true
// dayjs
dayjs ( '2010-10-20' ) . isBefore ( '2010-10-21' ) ;
// => true
// luxon
DateTime . fromISO ( '2010-10-20' ) < DateTime . fromISO ( '2010-10-21' ) ;
// => true
// Temporal
Temporal . PlainDate . compare ( '2010-10-20' , '2010-10-21' ) === - 1 ;
// => true⬆กลับไปด้านบน
ตรวจสอบว่าวันที่เหมือนกับวันที่อื่นหรือไม่
// Moment.js
moment ( '2010-10-20' ) . isSame ( '2010-10-21' ) ;
// => false
moment ( '2010-10-20' ) . isSame ( '2010-10-20' ) ;
// => true
moment ( '2010-10-20' ) . isSame ( '2010-10-21' , 'month' ) ;
// => true
// Native
new Date ( 2010 , 9 , 20 ) . valueOf ( ) === new Date ( 2010 , 9 , 21 ) . valueOf ( ) ;
// => false
new Date ( 2010 , 9 , 20 ) . valueOf ( ) === new Date ( 2010 , 9 , 20 ) . valueOf ( ) ;
// => true
new Date ( 2010 , 9 , 20 ) . getTime ( ) === new Date ( 2010 , 9 , 20 ) . getTime ( ) ;
// => true
new Date ( 2010 , 9 , 20 ) . valueOf ( ) === new Date ( 2010 , 9 , 20 ) . getTime ( ) ;
// => true
new Date ( 2010 , 9 , 20 ) . toDateString ( ) . substring ( 4 , 7 ) ===
new Date ( 2010 , 9 , 21 ) . toDateString ( ) . substring ( 4 , 7 ) ;
// => true
// date-fns
import isSameDay from 'date-fns/isSameDay' ;
import isSameMonth from 'date-fns/isSameMonth' ;
isSameDay ( new Date ( 2010 , 9 , 20 ) , new Date ( 2010 , 9 , 21 ) ) ;
// => false
isSameDay ( new Date ( 2010 , 9 , 20 ) , new Date ( 2010 , 9 , 20 ) ) ;
// => true
isSameMonth ( new Date ( 2010 , 9 , 20 ) , new Date ( 2010 , 9 , 21 ) ) ;
// => true
// dayjs
dayjs ( '2010-10-20' ) . isSame ( '2010-10-21' ) ;
// => false
dayjs ( '2010-10-20' ) . isSame ( '2010-10-20' ) ;
// => true
dayjs ( '2010-10-20' ) . isSame ( '2010-10-21' , 'month' ) ;
// => true
// luxon
( + DateTime . fromISO ( '2010-10-20' ) ===
+ DateTime . fromISO ( '2010-10-21' ) +
// => false
DateTime . fromISO ( '2010-10-20' ) ) ===
+ DateTime . fromISO ( '2010-10-20' ) ;
// => true
DateTime . fromISO ( '2010-10-20' ) . hasSame ( DateTime . fromISO ( '2010-10-21' ) , 'month' ) ;
// => true
// Temporal
Temporal . PlainDate . from ( '2010-10-20' ) . equals ( '2010-10-21' ) ;
// => false
Temporal . PlainDate . from ( '2010-10-20' ) . equals ( '2010-10-20' ) ;
// => true
Temporal . PlainDate . from ( '2010-10-20' ) . month === Temporal . PlainDate . from ( '2010-10-21' ) . month ;
// => true⬆กลับไปด้านบน
ตรวจสอบว่าวันที่หลังจากวันที่อื่น
// Moment.js
moment ( '2010-10-20' ) . isAfter ( '2010-10-19' ) ;
// => true
// Native
new Date ( 2010 , 9 , 20 ) > new Date ( 2010 , 9 , 19 ) ;
// => true
// date-fns
import isAfter from 'date-fns/isAfter' ;
isAfter ( new Date ( 2010 , 9 , 20 ) , new Date ( 2010 , 9 , 19 ) ) ;
// => true
// dayjs
dayjs ( '2010-10-20' ) . isAfter ( '2010-10-19' ) ;
// => true
// luxon
DateTime . fromISO ( '2010-10-20' ) > DateTime . fromISO ( '2010-10-19' ) ;
// => true
// Temporal
Temporal . PlainDate . compare ( '2010-10-20' , '2010-10-19' ) === 1 ;
// => true⬆กลับไปด้านบน
ตรวจสอบว่าวันที่อยู่ระหว่างสองวันอื่น ๆ
// Moment.js
moment ( '2010-10-20' ) . isBetween ( '2010-10-19' , '2010-10-25' ) ;
// => true
// date-fns
import isWithinInterval from 'date-fns/isWithinInterval' ;
isWithinInterval ( new Date ( 2010 , 9 , 20 ) , {
start : new Date ( 2010 , 9 , 19 ) ,
end : new Date ( 2010 , 9 , 25 ) ,
} ) ;
// => true
// dayjs ️ requires isBetween plugin
import isBetween from 'dayjs/plugin/isBetween' ;
dayjs . extend ( isBetween ) ;
dayjs ( '2010-10-20' ) . isBetween ( '2010-10-19' , '2010-10-25' ) ;
// => true
// luxon
Interval . fromDateTimes (
DateTime . fromISO ( '2010-10-19' ) ,
DateTime . fromISO ( '2010-10-25' )
) . contains ( DateTime . fromISO ( '2010-10-20' ) ) ;
// => true⬆กลับไปด้านบน
ตรวจสอบว่าหนึ่งปีเป็นปีที่ก้าวกระโดด
// Moment.js
moment ( [ 2000 ] ) . isLeapYear ( ) ;
// => true
// Native
new Date ( 2000 , 1 , 29 ) . getDate ( ) === 29 ;
// => true
// date-fns
import isLeapYear from 'date-fns/isLeapYear' ;
isLeapYear ( new Date ( 2000 , 0 , 1 ) ) ;
// => true
// dayjs ️ requires isLeapYear plugin
import isLeapYear from 'dayjs/plugin/isLeapYear' ;
dayjs . extend ( isLeapYear ) ;
dayjs ( '2000-01-01' ) . isLeapYear ( ) ;
// => true
// luxon
expect ( DateTime . local ( 2000 ) . isInLeapYear ) . toBeTruthy ( ) ;
// => true
// Temporal
Temporal . PlainDate . from ( '2000-01-01' ) . inLeapYear ;
// => true⬆กลับไปด้านบน
ตรวจสอบว่าตัวแปรเป็นวัตถุวันที่ JS ดั้งเดิมหรือไม่
// Moment.js
moment . isDate ( new Date ( ) ) ;
// => true
// Native
new Date ( ) instanceof Date ;
// => true
// date-fns
import isDate from 'date-fns/isDate' ;
isDate ( new Date ( ) ) ;
// => true
// dayjs
dayjs ( new Date ( ) ) . isValid ( ) ;
// luxon
DateTime . local ( ) . isValid ;
// => true
// Temporal
new Date ( ) instanceof Date ;
Temporal . Now . plainTimeISO ( ) instanceof Temporal . PlainTime ;
Temporal . Now . plainDateISO ( ) instanceof Temporal . PlainDate ;
Temporal . Now . plainDateTimeISO ( ) instanceof Temporal . PlainDateTime ;
Temporal . Now . zonedDateTimeISO ( ) instanceof Temporal . ZonedDateTime ;
// => true⬆กลับไปด้านบน
มิกซ์