Skip to main content
Engineering LibreTexts

20.1: Date and Time

  • Page ID
    54387
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The date and time functions are combined into a single system service call. The date and time values can be obtained as character strings, as integers, or both simultaneously. The options for date and time as explained in the next section followed by an example.

    It must be noted that if the operating system has an incorrect date or time, the values returned to the program will also be incorrect.

    Date and Time Options

    The date and/or time values are are obtained from the operating system using the get_date_time() system service call. The argument or arguments for the system service call must specify at least one of the following options:

    • date = <character(8)>
    • time = <character(10)>
    • zone = <character(5)>
    • values = <integer values array>

    As noted, each option must provide a location of where to place the results of the specified size and date type. The options are comma separated, similar to the read and write calls noted in a previous chapter. At least one argument must be included in the call.

    The zone, or time zone, option will provide the time difference between local time and Coordinated Universal Time (UTC1). The character string will provide a result in hours:minutes format and the integer values will be in minutes only. However, the minutes can be easily converted to hours.

    The options and associated values returned as more fully described in the following table.

    Option Data Type Description
    date character(8) The string returned will be in the form YYYYMMDD, where YYYY is year, MM is month, and DD is date.
    time character(10) The string returned will be in the form HHMMSS.SSS where HH is hour, MM is minute, SS is second, and SSS is milliseconds.
    zone character(5) The string returned will be in the form of \(\pm\)HHMM, where HHMM is the time difference between local time and Coordination Universal Time.
    values integer array, 8 elements The values will be returned in the 8 value integer array as follows:
    • values(1) \(\rightarrow\) year
    • values(2) \(\rightarrow\) month (1-12)
    • values(3) \(\rightarrow\) date (1-31)
    • values(4) \(\rightarrow\) time zone difference (minutes)
    • values(5) \(\rightarrow\) hour (0-23)
    • values(6) \(\rightarrow\) minutes (0-59)
    • values(7) \(\rightarrow\) seconds (0-59)
    • values(8) \(\rightarrow\) milliseconds (0-999)

    Each argument is optional, but at least one argument must be included. Multiple arguments are allowed.

    Date and Time Example Program

    The following as an example program that obtains the date and time information from the operating system in various formats. The final results are shown for reference.

    !  Example program to obtain the date and time from the system.
    
    program timeDateExample
    
    ! ----------
    !  Declarations.
    
    implicit none
    integer, dimension(8) :: valuesArr
    character(len=8) :: today
    character(len=10) :: now
    character(len=5) :: myzone
    integer :: i
    
    ! ----------
    !  Display simple header.
    
        write (*,'(a)')                                      &
            "Example Program for Date and Time Functions."
    
    ! ----------
    ! Get date, time, and zone from system as characters.
    ! Display to screen for reference.
    
        call date_and_time(date=today)
        write (*,'(/a, a)') "Today is: ", today
    
        call date_and_time(time=now, zone=myzone)
        write (*,'(a, a)') "Time is: ", now
        write (*,'(a, a/)') "Time Zone is: ", myzone
    
    ! ----------
    !  Get all date values from the system as integers.
    !  Display to screen for reference.
    
        call date_and_time(values=valuesArr)
    
        write (*,'(a)') "Values Array:"
    
        write (*,'(a, i4)') "Date, Year:        ", valuesArr(1)
        write (*,'(a, i2)') "Date, Month:       ", valuesArr(2)
        write (*,'(a, i2)') "Date, Day:         ", valuesArr(3)
        write (*,'(a, i2)') "Time, Hour:        ", valuesArr(5)
        write (*,'(a, i2)') "Time, Minutes:     ", valuesArr(6)
        write (*,'(a, i2)') "Time, Seconds:     ", valuesArr(7)
        write (*,'(a, i3)') "Time, Millseconds: ", valuesArr(8)
    
        write (*,'(/,a, i8)')                                &
            "Time difference with UTC in minutes: ",         &
                valuesArr(4)
    
        write (*,'(a, i2, a1, i2.2 ,/)')                     &
            "Time difference with UTC in hours: ",           &
                valuesArr(4)/60, ":", mod(valuesArr(4), 60)
    
    end program timeDateExample
    

    While this program does not really use the time or date values for anything meaningful, it does provide an example of how the information is obtained for use in other, more complex programs. The output of this example program is shown as follows:

    Today is: 20131212
    Time is: 154032.491
    Time Zone is: -0800
    
    Values Array:
    Date, Year:        2013
    Date, Month:       12
    Date, Day:         12
    Time, Hour:        15
    Time, Minutes:     40
    Time, Seconds:     32
    Time, Millseconds: 491
    
    Time difference with UTC in minutes:     -480
    Time difference with UTC in hours: -8:00
    

    The UTC for Las Vegas, Nevada is indeed, -8 hours as shown. The results for the UTC will be based on the actual geographic location of where the system executing the program is located.


    1. For more information regarding coordinated universal time, refer to: http://en.Wikipedia.org/wiki/Coordinated_Universal_Time

    This page titled 20.1: Date and Time is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?