<p>moment (). fromNow ()λŠ” '잘λͺ»λœ λ‚ μ§œ'λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.</p>

에 λ§Œλ“  2018λ…„ 10μ›” 07일  Β·  10μ½”λ©˜νŠΈ  Β·  좜처: moment/moment

λ¬Έμ œμ— λŒ€ν•œ μ„€λͺ… 및 μž¬ν˜„ 단계 :
λ‹€μŒμ„ μ‚¬μš©ν•˜μ—¬ μˆœκ°„μ„ μ‚¬μš©ν•˜μ—¬ μ‚¬μš©μž κ³„μ •μ˜ λ‚˜μ΄λ₯Ό μ–»μŠ΅λ‹ˆλ‹€.
moment(user account created date here).fromNow() ν•˜μ§€λ§Œ '잘λͺ»λœ λ‚ μ§œ'λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. 이것은 이전에 μž‘λ™ν–ˆμ§€λ§Œ 더 이상 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 생성 된 λ‚ μ§œλŠ” 2018-06-07T22:54:07.310Z 와 μœ μ‚¬ν•©λ‹ˆλ‹€.
moment(date here).format() μ‚¬μš©ν•˜λ©΄ μ •μƒμ μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€.
μ‚¬μš© 된 λͺ¨λ“  λ³€μˆ˜μ˜ 값을 ν¬ν•¨ν•˜μ‹­μ‹œμ˜€.
fromNowλ₯Ό ν¬λ§·ν•˜κ³  μ‚¬μš©ν•˜λŠ” 데 μ‚¬μš©ν•˜λŠ” λ‚ μ§œλŠ” 2018-06-07T22:54:07.310Z
ν™˜κ²½:
Discord 봇
예 : OSX의 Chrome 49, Windows의 Internet Explorer 10, Ubuntu 16.0.4의 Node.JS 4.4.4
μœ„μ— λ‚˜μ—΄λœ ν•­λͺ©μ—λŠ” μˆœκ°„μ„ μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
특히 IOT μ• ν”Œλ¦¬μΌ€μ΄μ…˜κ³Ό 같은 비정상적인 ν™˜κ²½μ΄μžˆλŠ” 경우 λΈŒλΌμš°μ €μ™€ OSκ°€ λͺ¨λ‘ μ€‘μš”ν•©λ‹ˆλ‹€.

도움이 될 μˆ˜μžˆλŠ” 기타 정보 : https://i.imgur.com/4LFsZh3.png

  • μ½”λ“œκ°€ μ‹€ν–‰λ˜λŠ” μ»΄ν“¨ν„°μ˜ μ‹œκ°„λŒ€ μ„€μ • : PST
  • μ½”λ“œκ°€ μ‹€ν–‰ 된 μ‹œκ°„ 및 λ‚ μ§œ : 12:20 PM, 10/7/18
  • μ‚¬μš©μ€‘μΈ 기타 라이브러리 (TypeScript, Immutable.js λ“±) μ—†μŒ

문제λ₯Όλ³΄κ³ ν•˜λŠ” 경우 μ‚¬μš©μ€‘μΈ ν™˜κ²½μ—μ„œ λ‹€μŒ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜κ³  좜λ ₯을 ν¬ν•¨ν•˜μ‹­μ‹œμ˜€.

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)

2018 λ…„ 10 μ›” 7 일 μΌμš”μΌ 12:29:05 GMT-0700 (νƒœν‰μ–‘ 일광 μ ˆμ•½ μ‹œκ°„)
2018-10-7 12:29:05
420
2.22.2
λ¬Έμ œκ°€ μˆœκ°„μ— κ²©λ¦¬λ˜μ—ˆλŠ”μ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‹΅μž₯을 보내 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. NodeJSλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμœΌλ―€λ‘œ λ„€λΉ„κ²Œμ΄ν„° ν™˜κ²½μ΄ μ—†μŠ΅λ‹ˆλ‹€.

new Date ()). toString () Thu Jan 17 2019 06:53:11 GMT-0600 (쀑뢀 ν‘œμ€€μ‹œ)
new Date ()). toLocaleString () : 2019 λ…„ 1 μ›” 7 일 μ˜€μ „ 6:53:11
new Date ()). getTimezoneOffset () : 360
moment.version : 2.23.0
NodeJSλŠ” v11.6.0μž…λ‹ˆλ‹€.

λ‚˜λŠ” λͺ‡ 가지 ν…ŒμŠ€νŠΈλ₯Ό ν•΄μ™”κ³  μΆ©λŒμ€ Object.prototype μƒμ„±μžμž…λ‹ˆλ‹€.

Object.prototype.test = function () {
    return test
}

const moment = require('moment');

const time = moment('2017-08-08T18:29:07.000Z')
console.log(time.isValid()) // true
console.log(time.toNow()) // Invalid date
console.log(time.fromNow()) // Invalid date

λͺ¨λ“  10 λŒ“κΈ€

Discord Bot을 μ–΄λ–»κ²Œ μ‹€ν–‰ν•©λ‹ˆκΉŒ?

_ "이전에 μž‘λ™ν–ˆμ§€λ§Œ 더 이상 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."_
μž‘λ™ ν•œ μ΄ν›„λ‘œ λ³€κ²½λœ 사항이 μžˆμŠ΅λ‹ˆκΉŒ? 예λ₯Ό λ“€μ–΄ λΆˆν™” 봇이 μ‹€ν–‰λ˜λŠ” ν™˜κ²½μ„ μ—…λ°μ΄νŠΈ ν–ˆμŠ΅λ‹ˆκΉŒ? 'μ‚¬μš©μž 계정 생성 일'ν˜•μ‹μ΄ λ³€κ²½ λ˜μ—ˆμŠ΅λ‹ˆκΉŒ?

μ•„λž˜μ˜ 좜λ ₯을 ν‘œμ‹œν•˜λ„λ‘ discord 봇 μ½”λ“œλ₯Ό μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆκΉŒ (특히 ν•˜λ“œ μ½”λ”© 된 λ¬Έμžμ—΄ μ‚¬μš©) :

console.log('moment.format:', moment("2018-06-07T22:54:07.310Z").format());
console.log('moment.fromNow:', moment("2018-06-07T22:54:07.310Z").fromNow());

κ·Έ μ „μ—λŠ” μž‘λ™ μ€‘μ΄μ—ˆμŠ΅λ‹ˆλ‹€. μ½”λ“œκ°€ λ³€κ²½λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. λ΄‡μœΌλ‘œ ν™˜κ²½μ΄ λ³€κ²½λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. λ‚ μ§œκ°€ ꡬ문 λΆ„μ„λ˜λŠ” 방식도 λ™μΌν•˜κ²Œ λ³΄μž…λ‹ˆλ‹€.

μ½˜μ†”μ΄ 좜λ ₯ ν•œ λ‚΄μš©μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

moment.format: 2018-06-07T15:54:07-07:00
moment.fromNow: Invalid date

그리고 λ‚΄κ°€ 그것을 μ‚¬μš©ν•œ 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€ ( this.client.moment λŠ” μˆœκ°„μ„ μ •μ˜ν•œ λ°©λ²•μž…λ‹ˆλ‹€) :

 console.log('moment.format:', this.client.moment("2018-06-07T22:54:07.310Z").format());
console.log('moment.fromNow:', this.client.moment("2018-06-07T22:54:07.310Z").fromNow());

λ‚΄κ°€ μ–ΈκΈ‰ν–ˆλŠ”μ§€ μ—¬λΆ€λŠ” ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ μ‚¬μš©μ€‘μΈ ν˜„μž¬ 버전은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
"μˆœκ°„": "^ 2.22.2",
"moment-duration-format": "^ 2.2.2"
그리고 λ‚˜λŠ” λ…Έλ“œμ˜ v8.9.2에 μžˆμŠ΅λ‹ˆλ‹€.

Angular 6 (이온 기반 μ•±)κ³Ό λΉ„μŠ·ν•œ λ¬Έμ œκ°€ 있으며 μˆœκ°„ 2.22.2λŠ” 이와 같이 κ°„λ‹¨ν•œ 것에 λŒ€ν•΄ 잘λͺ»λœ λ‚ μ§œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
moment(new Date()).fromNow();
μ‘°μ–Έν•˜μ‹­μ‹œμ˜€.

2.23.0μ—μ„œ μ—¬μ „νžˆ λ°œμƒ

time = moment('2017-08-08T18:29:07.000Z')
time.isValid() // true
time.toNow() // Invalid date
time.fromNow() // Invalid date

이 μš”μ²­μ„ μ—΄μ–΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.
μ•ˆλ…•ν•˜μ„Έμš”! 이 문제λ₯Ό μ œμΆœν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€! λΆˆν–‰νžˆλ„ λ‚˜λŠ” 이것을 μž¬ν˜„ ν•  수 μ—†μŠ΅λ‹ˆλ‹€. λ‹€λ₯Έ 정보λ₯Ό 제곡 ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

μ»΄ν“¨ν„°μ—μ„œ λ‹€μŒ 쀄을 μ‹€ν–‰ν•˜λ©΄ λ„μ›€μ΄λ©λ‹ˆλ‹€.

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)

λ‹΅μž₯을 보내 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. NodeJSλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμœΌλ―€λ‘œ λ„€λΉ„κ²Œμ΄ν„° ν™˜κ²½μ΄ μ—†μŠ΅λ‹ˆλ‹€.

new Date ()). toString () Thu Jan 17 2019 06:53:11 GMT-0600 (쀑뢀 ν‘œμ€€μ‹œ)
new Date ()). toLocaleString () : 2019 λ…„ 1 μ›” 7 일 μ˜€μ „ 6:53:11
new Date ()). getTimezoneOffset () : 360
moment.version : 2.23.0
NodeJSλŠ” v11.6.0μž…λ‹ˆλ‹€.

λ‚˜λŠ” λͺ‡ 가지 ν…ŒμŠ€νŠΈλ₯Ό ν•΄μ™”κ³  μΆ©λŒμ€ Object.prototype μƒμ„±μžμž…λ‹ˆλ‹€.

Object.prototype.test = function () {
    return test
}

const moment = require('moment');

const time = moment('2017-08-08T18:29:07.000Z')
console.log(time.isValid()) // true
console.log(time.toNow()) // Invalid date
console.log(time.fromNow()) // Invalid date

λ‚˜λ„ μ΄κ²ƒμ˜ 영ν–₯μ„λ°›μŠ΅λ‹ˆλ‹€.
μ²˜μŒμ—λŠ” μƒμ„±μžμ—μ„œ λΆˆμ™„μ „ν•œ μœ λ‹‰μŠ€ νƒ€μž„ μŠ€νƒ¬ν”„ (데이터가 λ‚˜μ—κ²Œ μ œκ³΅λ˜λŠ” 방법)λ₯Ό μ‚¬μš©ν•˜λŠ” 것이 원인이라고 μƒκ°ν–ˆμ§€λ§Œ MomentJS λ¬Έμ„œμ˜ 예제 μ½”λ“œλ₯Ό ν…ŒμŠ€νŠΈ ν•œ κ²°κ³Ό fromNow() λŒ€ν•œ 호좜이 μžˆμŒμ„ λ°œκ²¬ν–ˆμŠ΅λ‹ˆλ‹€. Invalid Date λ°˜ν™˜ν–ˆμŠ΅λ‹ˆλ‹€.

여기에도 같은 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

toString Tue Jan 07 2020 10:20:09 GMT+0100 (Central European Standard Time)
toLocaleString 1/7/2020, 10:20:09 AM
getTimezoneOffset -60
navigator.userAgent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0
moment.version 2.24.0

"moment": "^2.24.0", λ™μΌν•©λ‹ˆλ‹€. λΈŒλΌμš°μ €μ—μ„œ μ‹€ν–‰ν•˜λ©΄ μ œλŒ€λ‘œ μž‘λ™ν•˜μ§€λ§Œ nodejsμ—μ„œλŠ” μ‹€νŒ¨ν•©λ‹ˆλ‹€. ν•΄κ²° 될 λ•ŒκΉŒμ§€ ν•΄κ²° 방법이 μžˆμŠ΅λ‹ˆκΉŒ? λΏ‘λΏ‘

ν•΄ν‚Ήμ΄μ§€λ§Œ Object.prototype ν™•μž₯을 λ³€κ²½ν•˜μ—¬ μ—΄κ±° ν•  수 없도둝 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

console.log(moment().fromNow()); // 'a few seconds ago'
Object.prototype.randomExtension = true;
console.log(moment().fromNow()); // 'Invalid date'

Object.keys(Object.prototype).forEach(function(extension) {
  Object.defineProperty(Object.prototype, extension, { enumerable: false });
});

console.log(moment().fromNow()); // 'a few seconds ago'
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰