함수 unixtime2datetime()

Jmnote (토론 | 기여)님의 2017년 4월 12일 (수) 11:17 판
함수 unixtime2datetime(), unixtime to datetime
함수 timestamp2datetime(), timestamp to datetime
unix timestamp to datetime
ts2dt
ut2dt

1 Bash

date +%Y-%m-%d\ %H:%M:%S -d @1406204196
# 2014-07-24 21:16:36
unixtime=1406204196
date +"%Y-%m-%d %H:%M:%S" --date @$unixtime
# 2014-07-24 21:16:36
unixtime=1406204196
datetime=`date +"%Y-%m-%d %H:%M:%S" --date @$unixtime`
echo $datetime
# 2014-07-24 21:16:36

2 C#

string dtStr = String.Format("{0:yyyy-MM-dd HH:mm:ss}", new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(1368597301));
// 2013-05-15 05:55:01
public static String ts2dt(double ts)
{
  DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(ts).ToLocalTime();
  return String.Format("{0:yyyy-MM-dd HH:mm:ss}", dtDateTime);
}
...
Console.Write( ts2dt(1368597301) );
// 2013-05-15 14:55:01

3 Excel

=1368597301/86400+25569
// 41409.24654 ← Format Cells: General
// 2013-05-15 05:55:01 ← Format Cells: (Custom) yyyy-mm-dd hh:mm:ss
=1368597301/60/60/24+DATE(1970,1,1)

4 JavaScript

var unixtime = 1406204196;
var datetime = new Date(unixtime*1000).toISOString().substring(0,19).replace(/T/,' ');
var unixtime = 1406204196;
var d = new Date(unixtime *1000);
var datetime = d.getFullYear()+"-"+("0"+(d.getMonth()+1)).slice(-2)+"-"+("0"+d.getDate()).slice(-2)+" "
  +("0"+d.getHours()).slice(-2)+":"+("0"+d.getMinutes()).slice(-2)+":"+("0"+d.getSeconds()).slice(-2);

5 PHP

echo date('Y-m-d H:i:s', 1368597301);
// 2013-05-15 14:55:01
$unixtime = 1406204196;
$datetime = date('Y-m-d H:i:s', $unixtime);
echo $datetime;
// 2014-07-24 21:16:36

6 Python

import datetime
ut = 1406204196
dt = datetime.datetime.fromtimestamp( ut ).strftime('%Y-%m-%d %H:%M:%S')
print dt
# 2014-07-24 21:16:36
import time
ut = 1406204196
dt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ut) )
print dt
# 2014-07-24 21:16:36

7 SQL

7.1 MySQL

SELECT FROM_UNIXTIME( 1368597301 );
# 2013-05-15 14:55:01

8 같이 보기