-
-
Save akexorcist/eb5e8266670721c191a62851e3d8bb3d to your computer and use it in GitHub Desktop.
| module.exports = { | |
| toBuddhistYear: (moment, format) => { | |
| var christianYear = moment.format('YYYY') | |
| var buddhishYear = (parseInt(christianYear) + 543).toString() | |
| return moment | |
| .format(format.replace('YYYY', buddhishYear).replace('YY', buddhishYear.substring(2, 4))) | |
| .replace(christianYear, buddhishYear) | |
| } | |
| } |
| const moment = require('moment-timezone') | |
| const { toBuddhistYear } = require('./buddhist-year') | |
| moment.locale('th') | |
| console.log(toBuddhistYear(moment().tz('Asia/Bangkok'), 'LLLL')) |
| { | |
| "name": "moment-experiment", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "mocha" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "moment": "^2.29.1", | |
| "moment-timezone": "^0.5.33" | |
| }, | |
| "devDependencies": { | |
| "mocha": "^6.2.3" | |
| } | |
| } |
| const assert = require('assert') | |
| const moment = require('moment-timezone') | |
| const { toBuddhistYear } = require('../buddhist-year') | |
| describe('ToBuddhistYear', () => { | |
| it('Should return corrent date and time in LLLL format', () => { | |
| assert.strictEqual('Thursday, June 27, 2562 2:15 AM', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'LLLL')) | |
| }) | |
| it('Should return correct date without time in LLLL format', () => { | |
| assert.strictEqual('Thursday, June 27, 2562 12:00 AM', toBuddhistYear(moment('2019-06-27'), 'LLLL')) | |
| }) | |
| it('Should return corrent date and time in llll format', () => { | |
| assert.strictEqual('Thu, Jun 27, 2562 2:15 AM', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'llll')) | |
| }) | |
| it('Should return correct date without time in llll format', () => { | |
| assert.strictEqual('Thu, Jun 27, 2562 12:00 AM', toBuddhistYear(moment('2019-06-27'), 'llll')) | |
| }) | |
| it('Should return corrent date and time in LLL format', () => { | |
| assert.strictEqual('June 27, 2562 2:15 AM', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'LLL')) | |
| }) | |
| it('Should return correct date without time in LLL format', () => { | |
| assert.strictEqual('June 27, 2562 12:00 AM', toBuddhistYear(moment('2019-06-27'), 'LLL')) | |
| }) | |
| it('Should return corrent date and time in lll format', () => { | |
| assert.strictEqual('Jun 27, 2562 2:15 AM', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'lll')) | |
| }) | |
| it('Should return correct date without time in lll format', () => { | |
| assert.strictEqual('Jun 27, 2562 12:00 AM', toBuddhistYear(moment('2019-06-27'), 'lll')) | |
| }) | |
| it('Should return corrent date and time with LL format', () => { | |
| assert.strictEqual('June 27, 2562', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'LL')) | |
| }) | |
| it('Should return correct date without time in LL format', () => { | |
| assert.strictEqual('June 27, 2562', toBuddhistYear(moment('2019-06-27'), 'LL')) | |
| }) | |
| it('Should return corrent date and time in ll format', () => { | |
| assert.strictEqual('Jun 27, 2562', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'll')) | |
| }) | |
| it('Should return correct date without time in ll format', () => { | |
| assert.strictEqual('Jun 27, 2562', toBuddhistYear(moment('2019-06-27'), 'll')) | |
| }) | |
| it('Should return corrent date and time in L format', () => { | |
| assert.strictEqual('06/27/2562', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'L')) | |
| }) | |
| it('Should return correct date without time in L format', () => { | |
| assert.strictEqual('06/27/2562', toBuddhistYear(moment('2019-06-27'), 'L')) | |
| }) | |
| it('Should return corrent date and time in l format', () => { | |
| assert.strictEqual('6/27/2562', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'l')) | |
| }) | |
| it('Should return correct date without time in l format', () => { | |
| assert.strictEqual('6/27/2562', toBuddhistYear(moment('2019-06-27'), 'l')) | |
| }) | |
| it('Should return corrent date and time in MM/DD/YYYY format', () => { | |
| assert.strictEqual('06/27/2562', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'MM/DD/YYYY')) | |
| }) | |
| it('Should return correct date without time in MM/DD/YYYY format', () => { | |
| assert.strictEqual('06/27/2562', toBuddhistYear(moment('2019-06-27'), 'MM/DD/YYYY')) | |
| }) | |
| it('Should return corrent date and time in MM/DD/YY format', () => { | |
| assert.strictEqual('06/27/62', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'MM/DD/YY')) | |
| }) | |
| it('Should return correct date without time in MM/DD/YY format', () => { | |
| assert.strictEqual('06/27/62', toBuddhistYear(moment('2019-06-27'), 'MM/DD/YY')) | |
| }) | |
| it('Should return corrent date and time in MM/DD/Y format', () => { | |
| assert.strictEqual('06/27/2562', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'MM/DD/Y')) | |
| }) | |
| it('Should return correct date without time in MM/DD/Y format', () => { | |
| assert.strictEqual('06/27/2562', toBuddhistYear(moment('2019-06-27'), 'MM/DD/Y')) | |
| }) | |
| it('Should return correct date without time in YYYY-MM-DDTHH:mm:ss.sssZ format', () => { | |
| assert.strictEqual('2562-06-27T02:15:07.077+07:00', toBuddhistYear(moment('2019-06-27T02:15:07+07:00'), 'YYYY-MM-DDTHH:mm:ss.sssZ')) | |
| }) | |
| it('Should return corrent date and time in MM/DD/Y format and leap day', () => { | |
| assert.strictEqual('02/29/2563', toBuddhistYear(moment('2020-02-29:15:07+07:00'), 'MM/DD/Y')) | |
| }) | |
| it('Should return correct date without time in MM/DD/Y format and leap day', () => { | |
| assert.strictEqual('02/29/2563', toBuddhistYear(moment('2020-02-29'), 'MM/DD/Y')) | |
| }) | |
| it('Should return correct date without time in YYYY-MM-DDTHH:mm:ss.sssZ format and leap day', () => { | |
| assert.strictEqual('2563-02-29T02:15:07.077+07:00', toBuddhistYear(moment('2020-02-29T02:15:07+07:00'), 'YYYY-MM-DDTHH:mm:ss.sssZ')) | |
| }) | |
| }) |
ขอบคุณมากครับ
เท่าที่อ่าน code ดู คิดว่า ไม่น่าจะรองรับ 29 Feb นะครับ เพราะว่า ถ้า 29/02/1980 + 543 จะได้เป็น invalid date ใน moment เพราะ moment จะมองว่า 29/02/2523 (ค.ศ.)ไม่มีอยู่จริง
เท่าที่อ่าน code ดู คิดว่า ไม่น่าจะรองรับ 29 Feb นะครับ เพราะว่า ถ้า 29/02/1980 + 543 จะได้เป็น invalid date ใน moment เพราะ moment จะมองว่า 29/02/2523 (ค.ศ.)ไม่มีอยู่จริง
เขา replace แค่ string ที่เป็น year ไม่ใช่หรอครับ ไม่ได้บวกปี คศ. ด้วย 543 ไปตรงๆ ซึ่งการ replace เฉพาะ string year มันคือการแปลงจาก คศ. เป็น พศ. ปกติอยู่แล้ว (หรือผมดูผิดนะ)
@ekaluckc ตามที่คุณ @ToeiKanta บอกเลยครับ ผมไม่ได้เอา 543 บวกเข้าไปในปีตรงๆครับ แต่ใช้วิธี Replace ใน String Year แทน
เพิ่มเทสสำหรับ Leap Days เข้าไปให้แล้วครับ
@ekaluckc ตามที่คุณ @ToeiKanta บอกเลยครับ ผมไม่ได้เอา 543 บวกเข้าไปในปีตรงๆครับ แต่ใช้วิธี Replace ใน String Year แทน
เพิ่มเทสสำหรับ Leap Days เข้าไปให้แล้วครับ
รับทราบครับ จริงด้วยครับ สุดยอด ขอบคุณมากครับ ขออภัยที่อ่าน code เร็วไปหน่อยครับ
ขอบคุณครับ