For Developers
How to get the current date and time in Fortran, still widely used today in scientific computing, climate modeling, and high-performance numerical code.
program get_time
integer :: values(8)
call date_and_time(values=values)
! values = [year, month, day, utc_offset_min, hour, minute, second, millisecond]
print *, values
end program
! Fortran has no built-in Unix-epoch conversion. Modern Fortran (2003+)
! exposes SYSTEM_CLOCK for elapsed timing and DATE_AND_TIME for calendar
! fields, but converting a raw Unix timestamp integer into those calendar
! fields requires writing the day-count-to-calendar algorithm by hand,
! or linking a C library function via Fortran's ISO_C_BINDING.
use iso_c_binding
interface
function time(t) bind(C, name="time")
import :: c_long
integer(c_long) :: time
integer(c_long) :: t
end function
end interface
! Similarly, going from calendar fields back to a Unix timestamp normally
! means calling out to the C standard library's mktime() via
! ISO_C_BINDING, rather than anything in Fortran's own intrinsics.
Fortran's intrinsic date/time support (DATE_AND_TIME, SYSTEM_CLOCK) was designed for scientific timing and calendar display, not for interoperating with Unix timestamps — there's no built-in function that returns seconds since 1970 the way virtually every other modern language provides directly. Numerical and scientific Fortran code that needs actual Unix epoch values typically calls out to C's time() and mktime() through ISO_C_BINDING (standardized since Fortran 2003), which is a heavier lift than a one-line call in most other languages, but is the reliable, portable way to do it rather than re-deriving epoch math from scratch.
Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.
Related
Every language's snippets side by side, for quick comparison.
Convert any timestamp instantly, no code required.
The C-family epoch functions Fortran calls into via ISO_C_BINDING.