Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"colors": true
},
"dependencies": {
"moment-timezone": "^0.5.34"
"luxon": "^3.0.1"
},
"devDependencies": {
"c8": "^7.11.3",
Expand Down
43 changes: 31 additions & 12 deletions src/CalDate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import moment from 'moment-timezone'
import { DateTime } from 'luxon'
import { toYear, toNumber, isDate, pad0 } from './utils.js'

const PROPS = ['year', 'month', 'day', 'hour', 'minute', 'second']
Expand Down Expand Up @@ -182,7 +181,22 @@ export class CalDate {
*/
toTimezone (timezone) {
if (timezone) {
return new Date(moment.tz(this.toString(), timezone).format())
return DateTime.fromObject(
{
day: this.day,
month: this.month,
year: this.year,
hour: this.hour,
minute: this.minute,
second: this.second,
millisecond: 0
},
{
zone: timezone
}
)
.toUTC()
.toJSDate()
} else {
return this.toDate()
}
Expand All @@ -196,13 +210,13 @@ export class CalDate {
*/
fromTimezone (dateUTC, timezone) {
if (timezone) {
const m = moment.tz(dateUTC, timezone)
this.year = m.year()
this.month = m.month() + 1
this.day = m.date()
this.hour = m.hours()
this.minute = m.minutes()
this.second = m.seconds()
const m = DateTime.fromJSDate(dateUTC, { zone: timezone })
this.year = m.year
this.month = m.month
this.day = m.day
this.hour = m.hour
this.minute = m.minute
this.second = m.second
} else {
this.set(dateUTC)
}
Expand All @@ -215,8 +229,13 @@ export class CalDate {
*/
toDate () {
return new Date(
this.year, this.month - 1, this.day,
this.hour, this.minute, this.second, 0
this.year,
this.month - 1,
this.day,
this.hour,
this.minute,
this.second,
0
)
}

Expand Down
18 changes: 18 additions & 0 deletions test/CalDate.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ describe('#CalDate', function () {
assert.strictEqual(res, '2000-01-01T05:00:00.000Z')
})

it('can move date by timezone Tokyo (march 2015)', function () {
const caldate = new CalDate(new Date('2015-03-21 00:00:00'))
const res = caldate.toTimezone('Asia/Tokyo').toISOString()
assert.strictEqual(res, '2015-03-20T15:00:00.000Z')
})

it('can move date by timezone Tokyo (sepetember 2015)', function () {
const caldate = new CalDate(new Date('2015-09-23 00:00:00'))
const res = caldate.toTimezone('Asia/Tokyo').toISOString()
assert.strictEqual(res, '2015-09-22T15:00:00.000Z')
})

it('can move date by timezone Tokyo (sepetember 2021)', function () {
const caldate = new CalDate(new Date('2021-09-23 00:00:00'))
const res = caldate.toTimezone('Asia/Tokyo').toISOString()
assert.strictEqual(res, '2021-09-22T15:00:00.000Z')
})

it('can return date in current timezone', function () {
const caldate = new CalDate({ year: 2000, month: 1, day: 1 })
const exp = new Date('2000-01-01 00:00:00')
Expand Down