Date Validation & Conversion

Given a date like 20/2/2002, we like to convert it to: Wed. Feb 20, 2002. The process involves validating the input to ensure it has the proper format and then extracting from it the day, month, and year as strings. Assuming the extracted fields are indeed numeric, we convert them to integers and then verify that the date exists; e.g. it is not the 31st of September or the 29th of February in a non-leap year. Once properly validated, we employ the Zeller algorithm to find the day of the week (dow) and then output the result looking up the month name and the day name.

Here is how the sought system should behave:

 Enter the date ...
 Invalid format!
 Enter the date ...
 Invalid format!
 Enter the date ...
 Non-numeric values!
 Enter the date ...
 None existent date!
 Enter the date ...
 Thu. Feb 10, 1983

It would be a mistake to attempt this using the un-structured approach. This would yield messy code that is hard to debug and understand. Instead, we break the problem into pieces as shown in the following structure chart:

Here is the full main program:

program LongDate
implicit none
character*10 date, day, month, year, readDate
integer*2 d, m, y
logical*1 ok, existDMY
do while (.true.)
 ok = .true.
 date = readDate()
 call checkFormat(date, day, month, year, ok)
 if (.not. ok) then
 print*, "Invalid format!"
 call checkValue(day, month, year, d, m, y, ok)
 if (.not. ok) then
 print*, "Non-numeric values!"
 if (existDMY(d, m, y)) then
 call printDate(d, m, y)
 print*, "None existent date!"
 ok = .false.
 end if
 end if
 end if
 if (ok) then
 end if
end do

The job description of each subprogram is as follows:

                 mm = m
                 yy = y
                 if (m .le. 2) then
                 mm = m + 12
                 yy = y - 1
                 end if
                 tmp1 = 26*(mm+1) / 10
                 tmp2 = 125*yy / 100
                 tmp3 = d + tmp1 + tmp2 - yy / 100 + yy / 400 - 1
  dow = mod(tmp3, 7)


It would also be a mistake if these subprograms are written in full, put together, and then tested. Instead, write only one of them, any one, and then write a small main program that uses it. Such a main program is known as a driver or a test harness and it enables you to test each piece before building the whole system. Only when all pieces are tested individually, and only then, would you put them together and test. You can put everything in one giant source file but it is recommended that those subprograms that can be useful for other applications (such as s2n or isLeap) be placed in separate files and compiled using f2lib.


  1. Printed program
  2. A log showing the program's input and output for at least 10 cases so that all possible outcomes (errors or valid results) are demonstrated.