Skip to content

Instantly share code, notes, and snippets.

@harunorimurata
Last active March 2, 2024 13:54
Show Gist options
  • Save harunorimurata/382f7f33376e394b5660faf451c19d03 to your computer and use it in GitHub Desktop.
Save harunorimurata/382f7f33376e394b5660faf451c19d03 to your computer and use it in GitHub Desktop.
const grades = ['小1', '小2', '小3', '小4', '小5', '小6', '中1', '中2', '中3'];
// 誕生日からat時点での学年を算出
const birthdayToSchoolGrade = (birthday, at) => {
const d = new Date(at);
d.setMonth(d.getMonth() - 3);
d.setMonth(3);
d.setDate(1);
const bd = new Date(birthday);
const dNum = Number(d.toISOString().substring(0, 10).replaceAll('-', ''));
const bdNum = Number(bd.toISOString().substring(0, 10).replaceAll('-', ''));
const i = Math.floor((dNum - bdNum) / 10000) - 6;
return grades[i];
};
// at時点で学年(grade)に属する生徒の生年月日がいつからいつまでか
const gradeToBirthdayRange = (grade, at) => {
const year = grades.indexOf(grade) + 7;
const start = new Date(at.getFullYear(), at.getMonth(), at.getDate());
start.setMonth(start.getMonth() - 3);
start.setYear(start.getFullYear() - year);
start.setMonth(3);
start.setDate(2);
const end = new Date(start);
end.setYear(start.getYear() + 1);
return [start, end];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment