@echo off

set /p "birthdate=生年月日を入力してください (YYYY/MM/DD): "
set "convertedDate="

REM 年の抽出
set "year=%birthdate:~2,2%"

REM 月と日の抽出
set "month=%birthdate:~5,2%"
set "day=%birthdate:~8%"

REM 月と日の桁数が1桁の場合、先頭に0を追加
if "%month:~0,1%"=="0" set "month=%month:~1%"
if "%day:~0,1%"=="0" set "day=%day:~1%"
REM 入力された年月日を指定の形式に結合
set "convertedDate=%year%%month%%day%"

REM 結果を表示
echo 変換された形式の日付: %convertedDate%

REM 生年月日の正当性をチェック
powershell -command "& {try { [DateTime]::ParseExact('%convertedDate%', 'yyMMdd', [System.Globalization.CultureInfo]::InvariantCulture) } catch { exit 1 }}"

if %errorlevel% neq 0 (
    echo 生年月日の形式が正しくありません。再度入力してください。
    exit /b
)

set /p "crimeDate=犯行日を入力してください (YYYY/MM/DD): "
set /p "arrestDate=起訴日を入力してください (YYYY/MM/DD): "

REM 犯行日の正当性をチェック
powershell -command "& {try { [DateTime]::ParseExact('%crimeDate%', 'yyyy/MM/dd', [System.Globalization.CultureInfo]::InvariantCulture) } catch { exit 1 }}"

if %errorlevel% neq 0 ( echo 犯行日の形式が正しくありません。再度入力してください。 exit /b ) REM 起訴日の正当性をチェック powershell -command "& {try { [DateTime]::ParseExact('%arrestDate%', 'yyyy/MM/dd', [System.Globalization.CultureInfo]::InvariantCulture) } catch { exit 1 }}" if %errorlevel% neq 0 ( echo 起訴日の形式が正しくありません。再度入力してください。 exit /b ) REM 犯行日における年齢の計算 powershell -command "& { $birthdate = [DateTime]::ParseExact('%convertedDate%', 'yyMMdd', [System.Globalization.CultureInfo]::InvariantCulture); $crimeDate = [DateTime]::ParseExact('%crimeDate%', 'yyyy/MM/dd', [System.Globalization.CultureInfo]::InvariantCulture); $ageAtCrime = $crimeDate.Year - $birthdate.Year; if ($crimeDate.Month -lt $birthdate.Month -or ($crimeDate.Month -eq $birthdate.Month -and $crimeDate.Day -lt $birthdate.Day)) { $ageAtCrime-- }; Write-Host '犯行日 (%crimeDate%) 時点での年齢は ' $ageAtCrime ' 歳です.' }"

REM 起訴日における
REM 年齢の計算
powershell -command "& { $birthdate = [DateTime]::ParseExact('%convertedDate%', 'yyMMdd', [System.Globalization.CultureInfo]::InvariantCulture); $arrestDate = [DateTime]::ParseExact('%arrestDate%', 'yyyy/MM/dd', [System.Globalization.CultureInfo]::InvariantCulture); $ageAtArrest = $arrestDate.Year - $birthdate.Year; if ($arrestDate.Month -lt $birthdate.Month -or ($arrestDate.Month -eq $birthdate.Month -and $arrestDate.Day -lt $birthdate.Day)) { $ageAtArrest-- }; Write-Host '起訴日 (%arrestDate%) 時点での年齢は ' $ageAtArrest ' 歳です.' }"

pause