Aside from using date and time related libraries and modules as enumerated at the Fortran Wiki and several stopwatch modules that are available, a (relatively) simple example code using DATE_AND_TIME() that contains a re-useable procedure for converting DATE_AND_TIME() values to a Julian date that is useful for timings up to the precision of DATE_AND_TIME():
program demo_date_to_julian
implicit none
! type for unix epoch time and julian days
integer,parameter :: realtime=kind(0.0d0)
integer :: dat(8)
real(kind=realtime) :: juliandate(2)
character :: paws
integer :: ierr,ios,i
do i=1,2
call date_and_time(values=dat)
! convert DAT to Julian Date
call date_to_julian(dat,juliandate(i),ierr)
write(*,*)'Julian Date is ',juliandate(i)
write(*,*)'ierr is ',ierr
if(i.eq.1)then
write(*,'(a)',advance='no')'enter [RETURN] to continue ...'
read(*,'(a)',iostat=ios)paws
endif
enddo
write(*,*)'Delta',juliandate(2)-juliandate(1)
contains
subroutine date_to_julian(dat,julian,ierr)
! Convert proleptic Gregorian DAT date-time array to Julian Date
! array like returned by DATE_AND_TIME(3f)
integer,intent(in) :: dat(8)
! Julian Date (non-negative, but may be non-integer)
real(kind=realtime),intent(out) :: julian
! Error return: 0 =successful execution,-1=invalid year,-2=invalid month,
! -3=invalid day -4=invalid date (29th Feb, non leap-year)
integer,intent(out) :: ierr
integer :: year, month, day, utc, hour, minute
real(kind=realtime) :: second
integer :: A, Y, M, JDN
year = dat(1)
month = dat(2)
day = dat(3)
utc = dat(4)*60 ! Delta from UTC, convert from minutes to seconds
hour = dat(5)
minute = dat(6)
second = dat(7)-utc+dat(8)/1000.0d0 ! correct for time zone and milliseconds
! and IERR is < 0
if(year==0 .or. year .lt. -4713) then
julian = -HUGE(99999_realtime) ! this is the date if an error occurs
ierr=-1
return
endif
! You must compute first the number of years (Y) and months (M) since
! March 1st -4800 (March 1, 4801 BC)
A=(14-month)/12 ! A will be 1 for January or February, and 0 for
! other months, with integer truncation
Y=year+4800-A
M=month+12*A-3 ! M will be 0 for March and 11 for February
! All years in the BC era must be converted to astronomical years,
! so that 1BC is year 0, 2 BC is year "-1", etc.
! Convert to a negative number, then increment towards zero
! Staring from a Gregorian calendar date
! intentional integer truncation
JDN=day + (153*M+2)/5 + 365*Y + Y/4 - Y/100 + Y/400 - 32045
! Finding the Julian Calendar date given the JDN (Julian day number)
! and time of day
julian=JDN + dble(hour-12)/24.0d0 + dble(minute)/1440.0d0 + second/86400.0d0
if(julian.lt.0.d0) then ! Julian Day must be non-negative
ierr=1
else
ierr=0
endif
end subroutine date_to_julian
end program demo_date_to_julian