# YYYYMMDD to Year, Month, Day Conversion

### Problem Statement

In data processing, the year, month and day information are usually written as yyyymmdd, where the first four digits are Year, the fifth and sixth digits are Month, and the last two digits are Day. For example, 19710428 means April 8, 1971, and 20000101 means January 1, 2000.

Write a program to read an integer in the form of yyyymmdd and extract the values of Year, Month and Day. Do it with an external subroutine.

### Solution

```! --------------------------------------------------------------------
! PROGRAM  YYYYMMDDConversion:
!    This program uses an external subroutine Conversion() to convert
! an integer value in the form of YYYYMMDD to Year, Month and Day.
! --------------------------------------------------------------------

PROGRAM  YYYYMMDDConversion
IMPLICIT  NONE

INTERFACE                            ! interface block
SUBROUTINE  Conversion(Number, Year, Month, Day)
INTEGER, INTENT(IN)  :: Number
INTEGER, INTENT(OUT) :: Year, Month, Day
END SUBROUTINE  Conversion
END INTERFACE

INTEGER :: YYYYMMDD, Y, M, D

DO                                   ! loop until a zero is seen
WRITE(*,*)  "A YYYYMMDD (e.g., 19971027) please (0 to stop) -> "
IF (YYYYMMDD == 0)  EXIT          ! if 0, then bail out

CALL  Conversion(YYYYMMDD, Y, M, D)    ! do conversation

WRITE(*,*)  "Year  = ", Y         ! display results
WRITE(*,*)  "Month = ", M
WRITE(*,*)  "Day   = ", D
WRITE(*,*)
END DO
END PROGRAM  YYYYMMDDConversion

! --------------------------------------------------------------------
! SUBROUTINE  Conversion():
!    This external subroutine takes an integer input Number in the
! form of YYYYMMDD and convert it to Year, Month and Day.
! --------------------------------------------------------------------

SUBROUTINE  Conversion(Number, Year, Month, Day)
IMPLICIT  NONE

INTEGER, INTENT(IN)  :: Number
INTEGER, INTENT(OUT) :: Year, Month, Day

Year  = Number / 10000
Month = MOD(Number, 10000) / 100
Day   = MOD(Number, 100)
END SUBROUTINE  Conversion
```

### Program Input and Output

The following is the output from the above program for the input 3.0, 6.0 and 8.0:
```A YYYYMMDD (e.g., 19971027) please (0 to stop) ->
19971026
Year  = 1997
Month = 10
Day   = 26

A YYYYMMDD (e.g., 19971027) please (0 to stop) ->
20160131
Year  = 2016
Month = 1
Day   = 31

A YYYYMMDD (e.g., 19971027) please (0 to stop) ->
19010103
Year  = 1901
Month = 1
Day   = 3

A YYYYMMDD (e.g., 19971027) please (0 to stop) ->
0
```

### Discussion

• Subroutine Conversion() has four INTEGER formal arguments. Number is an integer in the form of yyyymmdd and Year, Month and Day are the values for year, month and day. Therefore, Number is declared with INTENT(IN) and Year, Month and Day are declared with INTENT(OUT).
• To compute the value for Year, Number is divided by 10000. In this way, the last four digits are removed (i.e., 19971205/10000 is 1997).
• The value for Day is from the last two digits. It is the remainder of dividing Number by 100. For example, MOD(19971205,100) yields 5.
• Two extract the value for Month, first note that we have to cut the first four digits off so that yyyymmdd becomes mmdd. Then, dividing mmdd by 100 yields mm. This is done with MOD(Number,10000)/100, where MOD(Number,10000) retrieves mmdd and this result is divided by 100 yielding Month.
• The main program has an INTERFACE block containing the subroutine header and the declarations of all formal arguments.
• The main program keeps asking for an integer in the form of yyyymmdd, and CALLs subroutine Conversion() to perform the conversion until the input is a zero.