Skip to content

Instantly share code, notes, and snippets.

@ChinaXing
Last active August 29, 2015 13:57
Show Gist options
  • Save ChinaXing/9885103 to your computer and use it in GitHub Desktop.
Save ChinaXing/9885103 to your computer and use it in GitHub Desktop.
convert Epoch to date fromat

SHELL :

date -d @Epoch

MYSQL :

select DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval 1396240017 second),"%Y-%m-%d %H:%m:%s");

PHP date(output format, epoch); Output format example: 'r' = RFC 2822 date more ... Python import time first, then time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(epoch)) Replace time.localtime with time.gmtime for GMT time. More info Ruby Time.at(epoch) Perl Use the Perl Epoch routines Java String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); VBScript/ASP DateAdd("s", epoch, "01/01/1970 00:00:00") More info AutoIT _DateAdd("s", $EpochSeconds , "1970/01/01 00:00:00") Delphi myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. C Use the C Epoch Converter routines Objective-C NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:epoch]; NSLog(@"%@", date); R as.POSIXct(epoch, origin="1970-01-01", tz="GMT") MySQL FROM_UNIXTIME(epoch, optional output format) Default output format is YYY-MM-DD HH:MM:SS. If you need support for negative timestamps: DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval -315619200 second),"%Y-%m-%d") (replace -315619200 with epoch) more ... PostgreSQL PostgreSQL version 8.1 and higher: SELECT to_timestamp(epoch); More info Older versions: SELECT TIMESTAMP WITH TIME ZONE 'epoch' + epoch * INTERVAL '1 second'; SQLite SELECT datetime(epoch_to_convert, 'unixepoch'); or local timezone: SELECT datetime(epoch_to_convert, 'unixepoch', 'localtime'); Oracle PL/SQL SELECT to_date('01-JAN-1970','dd-mon-yyyy')+(1326357743/60/60/24) from dual Replace 1326357743 with epoch. SQL Server DATEADD(s, epoch, '1970-01-01 00:00:00') Microsoft Excel =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number). For other time zones: =((A1 +/- time zone adjustment) / 86400) + 25569. Crystal Reports DateAdd("s", {EpochTimeStampField}-14400, #1/1/1970 00:00:00#) -14400 used for Eastern Standard Time. See Time Zones. JavaScript Use the JavaScript Date object Tcl/Tk clock format 1325376000 More info Unix/Linux Shell date -d @1190000000 Replace 1190000000 with your epoch, needs recent version of 'date'. Replace '-d' with '-ud' for GMT/UTC time. PowerShell Function get-epochDate ($epochDate) { [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($epochDate)) }, then use: get-epochDate 1279152364. Works for Windows PowerShell v1 and v2 Other OS's Command line: perl -e "print scalar(localtime(epoch))" (If Perl is installed) Replace 'localtime' with 'gmtime' for GMT/UTC time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment