Created
February 15, 2020 08:27
-
-
Save ababup1192/a32260509550b6f3e456bcc34deba949 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
dateToFormattedString, | |
dateToFormattedStringForInterviewTime, | |
toUTC, | |
utcToJstDateText, | |
utcToJstTimeText, | |
} from './date'; | |
describe('toUTC', () => { | |
it('JSTのDateがUTCに変換される', () => { | |
const date = new Date('2019-04-01T09:00:00.00+09:00'); | |
const expected = new Date('2019-04-01T00:00:00.00Z'); | |
expect(toUTC(date).toDateString()).toBe(expected.toDateString()); | |
}); | |
it('UTCのDateはUTCのまま変わらない。', () => { | |
const date = new Date('2019-04-01T00:00:00.00Z'); | |
const expected = new Date('2019-04-01T00:00:00.00Z'); | |
expect(toUTC(date).toDateString()).toBe(expected.toDateString()); | |
}); | |
}); | |
describe('utcToJstDateText', () => { | |
it('UTCの日時がLocal日付に変換される。', () => | |
expect(utcToJstDateText(new Date('2019-04-01T00:00:00Z'))).toBe( | |
'2019/4/1', | |
)); | |
it('UTCの日時が日またいた日付に変換される。', () => | |
expect(utcToJstDateText(new Date('2019-04-03T15:00:00Z'))).toBe( | |
'2019/4/4', | |
)); | |
}); | |
describe('utcToJstTimeText', () => { | |
it('UTCの日時がLocal日時に変換される。', () => | |
expect(utcToJstTimeText(new Date('2019-04-01T00:00:00Z'))).toBe('09:00')); | |
it('UTCの日時が日またいた日時に変換される。', () => | |
expect(utcToJstTimeText(new Date('2019-04-03T16:00:00Z'))).toBe('01:00')); | |
}); | |
describe('dateToFormattedString', () => { | |
it('UTCの日時が M/D (ddd) に変換される', () => { | |
expect(dateToFormattedString(new Date('2019-04-01T00:00:00Z'))).toBe( | |
'4/1 (月)', | |
); | |
}); | |
it('UTCの日時が日を跨いで M/D (ddd) に変換される', () => { | |
expect(dateToFormattedString(new Date('2019-04-01T16:00:00Z'))).toBe( | |
'4/2 (火)', | |
); | |
}); | |
}); | |
describe('dateToFormattedStringForInterviewTime', () => { | |
it('UTCの日時が YYYY/MM/DD (ddd) HH:mm に変換される', () => { | |
expect( | |
dateToFormattedStringForInterviewTime(new Date('2019-04-01T00:00:00Z')), | |
).toBe('2019/04/01 (月) 09:00'); | |
}); | |
it('UTCの日時が日を跨いで YYYY/MM/DD (ddd) HH:mm に変換される', () => { | |
expect( | |
dateToFormattedStringForInterviewTime(new Date('2019-04-01T16:00:00Z')), | |
).toBe('2019/04/02 (火) 01:00'); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dayjs/locale/ja'; | |
import dayjs from 'dayjs'; | |
import dayjsPluginUTC from 'dayjs-plugin-utc'; | |
dayjs.extend(dayjsPluginUTC); | |
dayjs.locale('ja'); | |
export type Term = { | |
startAt: Date; | |
endAt: Date; | |
}; | |
export const toUTC = (date: Date): Date => new Date(date.toUTCString()); | |
export const utcToJstDateText = (utcDate: Date): string => | |
dayjs(utcDate) | |
// NOTE(akutsu): dayjs-plugin-utcではoffsetを分で指定するため 9h * 60min = 540min で540を渡している | |
// @ts-ignore | |
.utcOffset(540) | |
.format('YYYY/M/D'); | |
export const utcToJstTimeText = (utcDate: Date): string => | |
dayjs(utcDate) | |
// @ts-ignore | |
.utcOffset(540) | |
.format('HH:mm'); | |
export const toTermText = (term: Term): string => { | |
const { startAt, endAt } = term; | |
const startDate = utcToJstDateText(startAt); | |
const endDate = utcToJstDateText(endAt); | |
const startTime = utcToJstTimeText(startAt); | |
const endTime = utcToJstTimeText(endAt); | |
return startDate === endDate | |
? `${startDate} ${startTime} 〜 ${endTime}` | |
: `${startDate} ${startTime} 〜 ${endDate} ${endTime}`; | |
}; | |
export const dateToFormattedString = (time: Date): string => { | |
return ( | |
dayjs(time) | |
// @ts-ignore | |
.utcOffset(540) | |
.format('M/D (ddd)') | |
); | |
}; | |
export const dateToFormattedStringForInterviewTime = (utcDate: Date): string => | |
dayjs(utcDate) | |
// @ts-ignore | |
.utcOffset(540) | |
.format('YYYY/MM/DD (ddd) HH:mm'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment