How to Convert a 13 Digit Unix Timestamp to Date and Time

How to convert a 13 digit Unix Timestamp to Date and time?

This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date function:

echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54

converting 13-digit unixtime in ms to timestamp in python

You have a millisecond-precise timestamp so first divide it by 1000 then feed it to datetime.datetime.fromtimestamp() for local timezone (or pass datetime.tzinfo of the target timezone as a second argument) or datetime.datetime.utcfromtimestamp() for UTC. Finally, use datetime.datetime.strftime() to turn it into a string of your desired format.

import datetime

timestamp = "1523126888080"
your_dt = datetime.datetime.fromtimestamp(int(timestamp)/1000) # using the local timezone
print(your_dt.strftime("%Y-%m-%d %H:%M:%S")) # 2018-04-07 20:48:08, YMMV

how to convert 13 digit unix milliseconds to hour only in java

You can extract the hours of the full datetime represented by those epoch milliseconds by converting it to an object representing this moment in time and then use an offset or a time zone to get a specific date and time of day:

public static void main(String[] args) {
long millis = 1567024014000L;
Instant instant = Instant.ofEpochMilli(millis);
OffsetDateTime odt = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
LocalTime lt = odt.toLocalTime();
System.out.printf("epoch millis %d represent the time of day %s",
millis, lt.format(DateTimeFormatter.ISO_LOCAL_TIME));
System.out.println();
System.out.printf("Full datetime representation is %s and hours only would be %d",
odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME), lt.getHour());
}

The output is

epoch millis 1567024014000 represent the time of day 20:26:54
Full datetime representation is 2019-08-28T20:26:54Z and hours only would be 20

Convert timestamp (unix 13 digits) to datetime format of a complet column of a csv file using awk or sed

Since sample of expected output is not given so could only test it with given 1st column values, written and tested in GNU awk. You could use strftime function of awk, also since OP hs mentioned Input_file is a csv file so mentioning FS and OFS as , here.

awk 'BEGIN{FS=OFS=","} {$1=strftime("%Y/%m/%d %H:%M:%S",$1/1000)}1' Input_file

From man awk for strftime:

strftime([format [, timestamp[, utc-flag]]]) Format timestamp
according to the specification in format. If utc-flag is present and
is non-zero or non-null, the result is in UTC, otherwise the result is
in local time. The timestamp should be of the same form as returned
by systime(). If timestamp is missing, the current time of day is
used. If format is missing, a default format equivalent to
the output of date(1) is used. The default format is available in
PROCINFO["strftime"]. See the specification for the strftime()
function in ISO C for the format conversions that are guaranteed to be
available.

How to convert current datetime into 13 digits Unix timestamp?

do it like this

import time
import datetime

d = datetime.datetime.now()
unixtime = datetime.datetime.timestamp(d)*1000

print(unixtime)

or you just use time.time()

compute a timestamps based on 13-digit unixtime timestamp in ms in python

13-digit Unix-time is already in millisecond, so if you want to get 300 milliseconds or after, you only need to:

X = "1396226255964"
Y1 = int(X) - 300
print("Y1:", Y1)
Y2 = int(X) + 300
print("Y2:", Y2)

This is a bit unusual, X is usually supposed to be integer.



Related Topics



Leave a reply



Submit