# Converting timestamp into a human-readable date

## Recommended Posts

Hello,

As I found somewhere, 1354445684 is a Unix Timestamp, and equals Sun, 02 Dec 2012 10:54:44 +0000 translated into a human-readable date, I have this code for example 5181740 as a Timestamp but I don't know how to extract a valid date from it, is that available it autoit?

##### Share on other sites

MsgBox (0, "1234567890", _GetDateFromUnix (1354445684))

Func _GetDateFromUnix (\$nPosix)
Local \$nYear = 1970, \$nMon = 1, \$nDay = 1, \$nHour = 00, \$nMin = 00, \$nSec = 00, \$aNumDays = StringSplit ("31,28,31,30,31,30,31,31,30,31,30,31", ",")
While 1
If (Mod (\$nYear + 1, 400) = 0) Or (Mod (\$nYear + 1, 4) = 0 And Mod (\$nYear + 1, 100) <> 0) Then; is leap year
If \$nPosix < 31536000 + 86400 Then ExitLoop
\$nPosix -= 31536000 + 86400
\$nYear += 1
Else
If \$nPosix < 31536000 Then ExitLoop
\$nPosix -= 31536000
\$nYear += 1
EndIf
WEnd
While \$nPosix > 86400
\$nPosix -= 86400
\$nDay += 1
WEnd
While \$nPosix > 3600
\$nPosix -= 3600
\$nHour += 1
WEnd
While \$nPosix > 60
\$nPosix -= 60
\$nMin += 1
WEnd
\$nSec = \$nPosix
For \$i = 1 to 12
If \$nDay < \$aNumDays[\$i] Then ExitLoop
\$nDay -= \$aNumDays[\$i]
\$nMon += 1
Next
Return \$nDay & "/" & \$nMon & "/" & \$nYear & " " & \$nHour & ":" & \$nMin & ":" & \$nSec
EndFunc; ==> _GetDateFromUnix

from here:

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

##### Share on other sites

#include <Constants.au3>
#include <Date.au3>

Local \$iEpoch = _DateToEpoch(_NowCalc())
MsgBox(\$MB_SYSTEMMODAL, '', \$iEpoch)
MsgBox(\$MB_SYSTEMMODAL, '', _EpochToDate(\$iEpoch))

Func _DateToEpoch(\$sDate)
Return Int(_DateDiff('s', '1970/01/01 00:00:00', \$sDate))
EndFunc   ;==>_DateToEpoch

Func _EpochToDate(\$iEpoch)
EndFunc   ;==>_EpochToDate

##### Share on other sites

funkey,

A link is suffice, there is no need to duplicate the code unless there were relevant changes.

##### Share on other sites

The Helpfile actually contains this very example as well.

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Thank you all guys.

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...