Created
July 26, 2021 07:53
-
-
Save tonkotsuboy/a5f742ab1f8978351252dd73b4aa338b to your computer and use it in GitHub Desktop.
dayjsを使った日付のバリデート
This file contains 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 { validateDate } from "./validateDate"; | |
describe("有効な日付のテスト", () => { | |
it.each(["2021/01/01", "2021/12/31", "2021/02/01", "2000/02/29"])( | |
"%sは正しい日付である", | |
(date) => { | |
expect(validateDate(date, "YYYY/MM/DD")).toBe(true); | |
} | |
); | |
it.each(["2021/01/999", "2021/02/29", "2021/2/1", "あああ"])( | |
"%sは正しい日付ではない", | |
(date) => { | |
expect(validateDate(date, "YYYY/MM/DD")).toBe(false); | |
} | |
); | |
}); |
This file contains 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 from "dayjs"; | |
/** | |
* 日付が正しいかをチェックする | |
*/ | |
export const validateDate = (date: string, format: string): boolean => { | |
return dayjs(date, format).format(format) === date; | |
}; | |
// 参考 https://qiita.com/kubotak/items/3bbde3c976606493060d |
Author
tonkotsuboy
commented
Jul 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment