Converting unix timestamp (length 13) string to readable date in Python
Your timestamp
is not the 'classical' Unix timestamp (number of seconds since Jan 1st, 1970), as it is expressed in milliseconds.
You can translate it like this:
import datetime
timestamp_with_ms = 1491613677888
# We separate the 'ordinary' timestamp and the milliseconds
timestamp, ms = divmod(timestamp_with_ms, 1000)
#1491613677 888
# We create the datetime from the timestamp, we must add the
# milliseconds separately
dt = datetime.datetime.fromtimestamp(timestamp) + datetime.timedelta(milliseconds=ms)
formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
# With Python 3.6, you could use:
# formatted_time = dt.isoformat(sep=' ', timespec='milliseconds')
print(formatted_time)
# 2017-04-08 03:07:57.888
Edit: I hadn't noticed that fromtimestamp
accepts a float. So, we can simply do:
import datetime
timestamp_with_ms = 1491613677888
dt = datetime.datetime.fromtimestamp(timestamp_with_ms / 1000)
formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
# With Python 3.6, you could use:
# formatted_time = dt.isoformat(sep=' ', timespec='milliseconds')
print(formatted_time)
# 2017-04-08 03:07:57.888
Convert Unix timestamp to a date string
With date
from GNU coreutils you can do:
date -d "@$TIMESTAMP"
# date -d @0
Wed Dec 31 19:00:00 EST 1969
(From: BASH: Convert Unix Timestamp to a Date)
On OS X, use date -r
.
date -r "$TIMESTAMP"
Alternatively, use strftime()
. It's not available directly from the shell, but you can access it via gawk. The %c
specifier displays the timestamp in a locale-dependent manner.
echo "$TIMESTAMP" | gawk '{print strftime("%c", $0)}'
# echo 0 | gawk '{print strftime("%c", $0)}'
Wed 31 Dec 1969 07:00:00 PM EST
Convert Unix timestamp into human readable date using MySQL
Use FROM_UNIXTIME()
:
SELECT
FROM_UNIXTIME(timestamp)
FROM
your_table;
See also: MySQL documentation on FROM_UNIXTIME()
.
Converting a UNIX Timestamp to Formatted Date String
Try gmdate
like this:
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
Converting Unix timestamp to date time - different results for JS and Python
Your timestamp value has microsecond resolution.
51 years (since epoch) * 365 * 24 * 3600 will give roughly 1.6 billion seconds. Your value has additional 6 digits.
So in Python divide by 1000000 instead of 1000.
Converting epoch timestamp into a readable date
That epoch is in seconds, you need to multiply it by 1000.
Transform Unix time stamp to a readable date time format
You can use pd.to_datetime
with unit
argument
kline = kline.rename({0:"Time",1:"Open",
2:"Close",3:"High",
4:"Low",5:"Amount",6:"Volume"}, axis='columns')
kline['Time'] = pd.to_datetime(kline['Time'], unit='s')
# ^^^ Addition here
kline.set_index('Time', inplace=True)
Related Topics
How to Find the Duplicates in a List and Create Another List With Them
How to Convert All Strings in a List of Lists to Integers
How Do Python'S Any and All Functions Work
Multiple Assignment and Evaluation Order in Python
Read Subprocess Stdout Line by Line
Apply Multiple Functions to Multiple Groupby Columns
What Is the _Del_ Method and How to Call It
Install a Python Package into a Different Directory Using Pip
Asynchronous Requests With Python Requests
Installing Python Module Within Code
Word Boundary With Words Starting or Ending With Special Characters Gives Unexpected Results
How to Hide Output of Subprocess
How to Check Which Version of Python Is Running My Script
How to Understand Closure in a Lambda
Append Existing Excel Sheet With New Dataframe Using Python Pandas