æ¥ä»ãæä½ããå¿
è¦ããã£ãã®ã§ï¼ãã¤ãã®ããã« Moment.js ã使ããã¨ããï¼JS ãã«ãã¤ã³ã® Date
ã¯æä½ãè¡ãã«ã¯ãã¾ãã«ã使ãã¥ããã®ã§ï¼è£å©çãªã©ã¤ãã©ãªã使ãã®ãå®ç³ã«ãªã£ã¦ããï¼8 ããåã® Java ã§ãã Joda-Time ã¿ãããªåå¨ï¼
ãªãã¡ã¬ã³ã¹ãè¦ãããã« Moment.js ã®å ¬å¼ãã¼ã¸ã«è¡ã£ããï¼ãªã«ãã Luxon ã¨ããæ°ããã©ã¤ãã©ãªããããã¨ã«æ°ã¥ããï¼ãã㯠Next Moment.js çãªæ°ãã«æ¸ãç´ãããã©ã¤ãã©ãªããã:
ãããããã¨ã調ã¹ã¦ãããã¡ã«ä»ã®æ¥ä»æä½ã®ã©ã¤ãã©ãªãè¦ã¤ããï¼Day.js 㨠date-fns ã ï¼
ã©ãéãã®ã
ãããããã©ãéãã®ãï¼æ¦è¦ã§æ¯è¼ããã¨ããããç¹å¾´ãããããã«è¦ãã:
ã©ã¤ãã©ãª | ç¹å¾´ |
---|---|
Moment.js | ãã使ããã¦ããï¼moment() ã¨ããé¢æ°ããæ¥ä»ã®ãªãã¸ã§ã¯ããä½æãã¦ï¼ããããã¡ã½ãããã§ã¼ã³ã§æä½ãã§ãããã¨ãç¹å¾´ï¼jQuery Object ã¿ãããªã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ãªã£ã¦ããï¼ã¿ã¤ã ã¾ã¼ã³ã«ã¤ãã¦ã¯ã¢ããªã³ãå¿
è¦ï¼ |
Day.js | Moment.js ã¨äºæã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæã£ã¦ããã 2kB ã¨è»½éã«ãªã£ã¦ãããã¨ãç¹å¾´ |
Luxon | Moment.js ã¨åãã¨ããã§éçºããã¦ãããï¼ã¤ã³ã¿ã¼ãã§ã¤ã¹ãã¾ã£ããç°ãªã£ã¦ããï¼ãããããªãã¸ã§ã¯ãæåã®æ¥ä»æä½ã®ã©ã¤ãã©ãªã®ããã« DateTime , Interval , Duration ãªã©ã®å½¹å²ãã¨ã®ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ããæä½ãè¡ãã®ãç¹å¾´ï¼Immutable ã«ãªã£ã¦ãããã¨ãç¹å¾´ï¼ |
date-fns | é¢æ°åã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãç¹å¾´ï¼lodash ã®ããã«æå»æä½ã«å¯¾ãã種ã
ã®é¢æ°ãããï¼é常㮠Date ãªãã¸ã§ã¯ãã«å¯¾ãããã«ãã¼ã¨ãã¦åãã®ãç¹å¾´ |
ISO 8601 å½¢å¼ã®è¡¨ç¾ ããã®ã¤ã³ã¹ã¿ã³ã¹ã®ä½æã§æ¯è¼ããã¨ãããããããããããªã:
ã©ã¤ãã©ãª | ISO 8601 å½¢å¼ããã®ä½æ | è¿ã£ã¦ããå |
---|---|---|
Moment.js | import moment from 'moment'; |
Moment |
Day.js | import dayjs from 'dayjs'; |
DayJs |
Luxon | import DateTime from 'luxon/src/datetime.js'; |
DateTime |
date-fns | import parseISO from 'date-fns/parseISO'; |
Date (builtin) |
ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®éãããããããï¼
- Moment.js, Day.js ã¯ã©ã¡ãã
jQuery
,$
é¢æ°ã®ãããªãã¡ã¯ããªé¢æ°ã«æååã渡ããã¨ã§æé»çã«ãã¼ã¹ãããï¼ - Luxon ã¯æ示çã«
DateTime.fromISO
ã¨ããstatic
ãªãã¡ã¯ããªã¡ã½ãããçµç±ãã¦ä½æããï¼ - date-fns ã¯
parseISO
ã¨ãããã¼ã¹ããé¢æ°ãimport
ãã¦ä½¿ãï¼ãã«ãã¤ã³ã®Date
ã¤ã³ã¹ã¿ã³ã¹ãä½æããï¼
ã¨ãããã®ã«ãªã£ã¦ããï¼
èå¯
(1) ã¯ãªãã§ãåãåããã¡ã¯ããªé¢æ°å
ã§ï¼ä¸ããããå¼æ°ã«å¿ãã¦æé»çã«å¤æããããªã¼ãã¼ãã¼ãé¢æ°ããããã®ããã«ãµãã¾ãï¼å¤ãã©ãããããªãã¸ã§ã¯ããè¿ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã ï¼ã¡ããã© jQuery ã® $(...)
ã lodash ã® _(...)
ã®ãããªã©ããã¼ã®ã©ã¤ãã©ãªã«ãã¾ã«è¦ããããã¤ï¼
(2) ã¯ããããæ¨æºçãªãªãã¸ã§ã¯ãæåã®ã©ã¤ãã©ãªã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã ã¨æãï¼ä¸ããå¤ã®æå³ã«å¿ãã¦ä½¿ç¨è
å´ãæ示çã«ã¯ã©ã¹ã®ãã¡ã¯ããªã¡ã½ãããå¼ã³åºãã¦çæããããã«ãªã£ã¦ããï¼è¿ãå¤ã DateTime
ã¨æ示çã§å
·ä½çã«ãã®ã«ãªã£ã¦ããï¼
(3) ã¯ãã«ãã¼é¢æ°ãé©ç¨ããã ãã«ãªã£ã¦ããï¼è¿ã£ã¦ãããªãã¸ã§ã¯ãããã«ãã¤ã³ã® Date
ã«ãªã£ã¦ããï¼ã ããã©ã¤ãã©ãªç¹æã®ã©ããã¼ã¯ã©ã¹ã使ããªãããã«ãªã£ã¦ããï¼
date-fns
ã®ãªã«ãé¢ç½ãã
ãã®ä¸ã§ date-fns ãé¢ç½ãã¨æã£ãçç±ã¯ï¼ãã®é¢æ°åã®ã¢ããã¼ãã«ããï¼
ãã¨ãã°ä»ã®ã©ã¤ãã©ãªåæ§ã«ï¼éå§æ¥æã¨çµäºæ¥æãæ㤠Interval
ã¨ãããã®ãããï¼ããã¯ãã¨ãã° 2020-04-01 12:00
ã 2020-05-01 12:00
ã¾ã§ã¨ããæ¥æã®ç¯å²ãããããã®ã«ä½¿ããï¼ããã«ã欲ãããªããããªæ¦å¿µã ï¼
dete-fns ã¯ãã® Interval
ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã¿å®ç¾©ãã¦ããï¼å
·ä½çãªã©ããã¼ã¯ã©ã¹ã§ã¯ãªãï¼ããã¾ã§è¦æ±ããåç¹æ§ã«ãªã£ã¦ãã:
type Interval = { start: Date | number end: Date | number }
date-fns/typings.d.ts at 21088144134ab581ea3a1f77485c645781534198 · date-fns/date-fns · GitHub
ã ããï¼ãã®ç¹æ§ãã¨ããªãã¸ã§ã¯ãã§ããã° Interval
ã¨ãã¦ä¸ãããã¨ãã§ããããã«ãªã£ã¦ããï¼ãããé¢ç½ãç¹ã«ãªã£ã¦ããï¼
// ok const int1 = { start: parseISO('2020-05-01T00:00:00'), // Date end: parseISO('2020-07-01T00:00:00'), // Date }; const dur1 = intervalToDuration(int1); console.log(dur1); // Object {days: 0, hours: 0, minutes: 0, months: 2, seconds: 0, years: 0}
https://runkit.com/mangano-ito/5ec2128c129f090013b3d455
åæ§ã« intervalToDuration
ã®è¿ãå¤ã¨ã㦠Interval
ããè¨ç®ãããæéããããã Duration
ãåã®ã¿ãå®ç¾©ãã¦ããï¼
ãã¡ããåãã¤ã³ã¿ã¼ãã§ã¤ã¹ããã¤ã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ã§ãå¯ã (Object
ã ã):
// this is also ok. const int2 = new class { get start() { return parseISO('2020-05-01T00:00:00'); } get end() { return parseISO('2020-07-01T00:00:00'); } }; const dur2 = intervalToDuration(int2); console.log(dur2);
https://runkit.com/mangano-ito/5ec21304a57d1b001ab1eeca
éæ¹é ã ãã© Array.prototype
ã« getter
ãçããã¦ãããã:
const int3 = [ parseISO('2020-05-01T00:00:00'), parseISO('2020-07-01T00:00:00'), ]; Object.defineProperty( Array.prototype, 'start', { get: function() { return this[0]; }, }, ); Object.defineProperty( Array.prototype, 'end', { get: function() { return this[1]; }, }, ); // this is ok, too! const dur3 = intervalToDuration(int3); console.log(dur3);
https://runkit.com/mangano-ito/5ec2132e25d80b001b43d325
ä»æ¹ï¼Luxon 㯠Interval
çãã©ã¤ãã©ãªã®ã©ããã¼ã¯ã©ã¹ã¨ãã¦ããï¼
const int1 = luxon.Interval.fromDateTimes( luxon.DateTime.fromISO('2020-05-01T00:00:00'), luxon.DateTime.fromISO('2020-07-01T00:00:00'), ); const dur1 = int1.toDuration(); console.log(dur1.toFormat("M 'months'")); // "2 months"
https://runkit.com/mangano-ito/5ec211dbb51f20001a795e52
Moment.js ã® Interval
㨠Duration
ã¯ç¬ç¹ã :
const dur1 = moment('2020-05-01T00:00:00').to(moment('2020-07-01T00:00:00')); console.log(dur1); // "in 2 months"
https://runkit.com/mangano-ito/5ec212564de79e001ba6e774
Day.JS ã§ã¯ RelativeTime
ãã©ã°ã¤ã³ãå¿
è¦ã ï¼(ã©ã¤ãã©ãªã®è»½éåã®ããããªï¼)
ææ³
æãä½ã«è¨ãã°ï¼ãªãã¸ã§ã¯ãæåã®ã¢ããã¼ãã¨é¢æ°åã®ã¢ããã¼ãã®éãï¼ã¨ããã ããããããªããï¼date-fns 㯠JavaScript ã®ç¹æ§ã«ãã¾ãåã£ã lodash ã®ãããªããªã·ã¼ãæã¤æ¥ææä½ã®ã©ã¤ãã©ãªã¨ãã¦é¢ç½ãã¨æã£ãï¼
ããã¦ï¼ãã¡ã¤ã³å
ã«å¤é¨ã©ã¤ãã©ãªã®æ¦å¿µãæã¡è¾¼ã¾ãªãã§ããã¨ããã«è¯ããæããï¼æ±ããã¼ã¿ã¯ç´ æ´ãªæ§é ä½ã§ãããã¨ã«ãªã£ã¦ããï¼ã©ã¤ãã©ãªã¯ããã¾ã§ã¤ã³ã¿ã¼ãã§ã¤ã¹ã«å¯¾ããã¦ã¼ã¹ã±ã¼ã¹ã ãã®ã·ã³ãã«ãªè¨è¨ã«ãªã£ã¦ããï¼struct
, trait
, impl
ã¿ãããªãããï¼
ããããªãããªãã¸ã§ã¯ãæååã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã»ãã使ããããã¨ããã®ã¯ãããããããªãï¼è£å®ãå¹ããããã ãããï¼å¤é¨ã®ã¢ãã«ãæã¡è¾¼ãã§ãã®ã¾ã¾åæãã¦ä½¿ããã¨ããã®ã¯æ¥½ã ã£ããããï¼ãªã®ã§å¥ã«é¢æ°åè³ä¸ä¸»ç¾©ã¨ããããã§ã¯ãªãï¼
çè«è²ã®çµè«ï¼ãããã«ããçã® Date
ãæã§è¨ç®ããã®ã¯è¾ãã®ã§ããããï¼