How to Convert a Unix Timestamp to Datetime and Vice Versa

How can I convert a Unix timestamp to DateTime and vice versa?

Here's what you need:

public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )
{
// Unix timestamp is seconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
return dateTime;
}

Or, for Java (which is different because the timestamp is in milliseconds, not seconds):

public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )
{
// Java timestamp is milliseconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
return dateTime;
}

How to Convert Timestamp to DateTime?

Your time is a Unix time in microseconds.

If you are using .Net 4.6 or later, you can convert this to a DateTime as follows.

long time = 1540787809621000; // Unix time in microseconds.

time /= 1000; // Divide by 1,000 because we need milliseconds, not microseconds.

DateTime result = DateTimeOffset.FromUnixTimeMilliseconds(time).DateTime;

Console.WriteLine(result); // Prints 29/10/2018 04:36:49 (UK format)

C# - Convert TimeStamp to DateTime

Unix timestamp - is amount of seconds from 1/1/1970, so just calc it:

static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}

how convert unix timestamp to datetime

Your code is working just fine, as is. Here is a fiddle.

Everyone that is telling you to use AddSeconds is wrong. The number you are giving us is clearly in milliseconds. There are 31,536,000 seconds in a year. 1415115303410 divided by 31536000 is 4487. There hasn't been 4,487 years passed since 1/1/1970.

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.

Convert unix timestamp in Python to datetime and make 2 Hours behind

With datetime.timedelta object:

from datetime import datetime, timedelta

unix_ts = 1507126064
dt = (datetime.fromtimestamp(unix_ts) - timedelta(hours=2)).strftime('%Y-%m-%d %H:%M:%S')
print(dt)

Convert Unix timestamp into datetime

You have to convert from Factor to Character To Number before using as.POSIXct. The function is expecting an integer as a Unixtime

head(as.POSIXct(as.numeric(as.character(try$time)), origin="1970-01-01", tz="GMT"))

How to convert a Unix timestamp to 2000-01-01 or 2000-05-24 20:00:00 format or reverse in Deno?

Aside from the standard Javascript ways, there is a date/time library for Deno called Ptera, that can be used as follows:

import { datetime } from "https://deno.land/x/ptera/mod.ts";

const dt = datetime("2000-05-24 20:00:00");
console.log(dt.format("X")); // X for Unix timestamp in seconds 959198400
console.log(dt.format("x")); // x for "Unix timestamp" in milliseconds 959198400000


const dt2 = datetime(1646245390158);
console.log(dt2.format("YYYY-MM-dd HH:mm:ss")); // output: 2022-03-02 19:23:10

A UNIX timestamp is the number of seconds since 1970-01-01 00:00:00 UTC, the Javascript timestamp is in milliseconds and sometimes in documentations, they also call this as a UNIX timestamp or UNIX Epoch time.

A detailed reference about the formatting options is available here.



Related Topics



Leave a reply



Submit