Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Last active April 25, 2024 10:20
Show Gist options
  • Save akexorcist/eb5e8266670721c191a62851e3d8bb3d to your computer and use it in GitHub Desktop.
Save akexorcist/eb5e8266670721c191a62851e3d8bb3d to your computer and use it in GitHub Desktop.
MomentJS with Buddhist year
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'))
})
})
@ekaluckc
Copy link

@ekaluckc ตามที่คุณ @ToeiKanta บอกเลยครับ ผมไม่ได้เอา 543 บวกเข้าไปในปีตรงๆครับ แต่ใช้วิธี Replace ใน String Year แทน

เพิ่มเทสสำหรับ Leap Days เข้าไปให้แล้วครับ

รับทราบครับ จริงด้วยครับ สุดยอด ขอบคุณมากครับ ขออภัยที่อ่าน code เร็วไปหน่อยครับ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment