JSI Tip 7323. How do I convert the number of days that have elapsed since the 1st of January 1970 to a date?

Date attributes in Active Directory are stored as a large integer that represents the number of seconds elapsed since 00:00:00, January 1, 1970 (UTC).

Ritchie Lawrence has scripted DaysToDate.bat, which contains:

@echo off
:: DaysToDate %days% yy mm dd
:: By: Ritchie Lawrence, 2002-06-15. Version 1.1
:: Func: Returns a calendar date from the number of elapsed days since 1st
::       January 1970 UTC. For NT4/2K/XP.
:: Args: %1 MJD used to create calendar date (by val)
::       %2 var to receive year component, 4 digits (by ref)
::       %3 var to receive month component, 2 digits, 01-12 (by ref)
::       %4 var to receive day of month component, 2 digits, 01-31 (by ref)
:: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%
NOTE: See How do I determine the number of days that have elapsed since the 1st of January 1970 for a given date?

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.