Convert a Unix timestamp to time in JavaScript
let unix_timestamp = 1549312452// Create a new JavaScript Date object based on the timestamp// multiplied by 1000 so that the argument is in milliseconds, not seconds.var date = new Date(unix_timestamp * 1000);// Hours part from the timestampvar hours = date.getHours();// Minutes part from the timestampvar minutes = "0" + date.getMinutes();// Seconds part from the timestampvar seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 formatvar formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
Convert UTC Epoch to local date
I think I have a simpler solution -- set the initial date to the epoch and add UTC units. Say you have a UTC epoch var stored in seconds. How about 1234567890
. To convert that to a proper date in the local time zone:
var utcSeconds = 1234567890;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);
d
is now a date (in my time zone) set to Fri Feb 13 2009 18:31:30 GMT-0500 (EST)
Convert UNIX epoch to Date object
Go via POSIXct
and you want to set a TZ
there -- here you see my (Chicago) default:
R> val <- 1352068320
R> as.POSIXct(val, origin="1970-01-01")
[1] "2012-11-04 22:32:00 CST"
R> as.Date(as.POSIXct(val, origin="1970-01-01"))
[1] "2012-11-05"
R>
Edit: A few years later, we can now use the anytime package:
R> library(anytime)
R> anytime(1352068320)
[1] "2012-11-04 16:32:00 CST"
R> anydate(1352068320)
[1] "2012-11-04"
R>
Note how all this works without any format or origin arguments.
Convert UNIX timestamp to date time (javascript)
You have to multiply by 1000 as JavaScript counts in milliseconds since epoch (which is 01/01/1970), not seconds :
var d = new Date(timestamp*1000);
Reference
How to convert unix timestamp to calendar date moment.js
Using moment.js as you asked, there is a unix
method that accepts unix timestamps in seconds:
var dateString = moment.unix(value).format("MM/DD/YYYY");
Convert unix epoch time to a date in Apache Derby
If you have the seconds since unix epoch, use:
select
{fn TIMESTAMPADD(SQL_TSI_SECOND, 1453974057, TIMESTAMP('1970-01-01-00.00.00.000000')) } as DT
from sysibm.SYSDUMMY1
Just replace "sysibm.SYSDUMMY1" with your original table, and replace 1453974057 with your value.
When dealing with milliseconds it gets a bit more complicated because you can't just use TIMESTAMPADD directly (you get SQL state 22003: The resulting value is outside the range for the data type INTEGER.)
If you have the milliseconds since unix epoch, use:
select
--the following block converts milliseconds since linux epoch to a timestamp
{ fn TIMESTAMPADD(
SQL_TSI_FRAC_SECOND,
(
--add the millisecond component
1453974057235 - { fn TIMESTAMPDIFF(
SQL_TSI_SECOND,
TIMESTAMP('1970-01-01-00.00.00.000000'),
{ fn TIMESTAMPADD(SQL_TSI_SECOND, 1453974057235/1000, TIMESTAMP('1970-01-01-00.00.00.000000')) }
)} * 1000
) * 1000000,
{ fn TIMESTAMPADD(SQL_TSI_SECOND, 1453974057235/1000, TIMESTAMP('1970-01-01-00.00.00.000000')) }
)} as FINAL_DT
from SYSIBM.SYSDUMMY1
Just replace the 3 instances of 1453974057235 with your value.
Convert unix/epoch time to formatted date - unexpected date
1386580621268
is not a unix timestamp i.e. seconds since epoch for 9-12-2013 but milliseconds since epoch. Remove the *1000L
or divide the input by 1000.
Unix epoch time to Java Date object
How about just:
Date expiry = new Date(Long.parseLong(date));
EDIT: as per rde6173's answer and taking a closer look at the input specified in the question , "1081157732" appears to be a seconds-based epoch value so you'd want to multiply the long from parseLong() by 1000 to convert to milliseconds, which is what Java's Date constructor uses, so:
Date expiry = new Date(Long.parseLong(date) * 1000);
javascript convert from epoch string to Date object
var date = new Date(1318023197289);
And, since unixEpoch is simply epoch / 1000, you can similarly multiply the argument in the constructor by 1000.
Related Topics
Using Data.Table Package Inside My Own Package
How to Delete Multiple Values from a Vector
Chopping a String into a Vector of Fixed Width Character Elements
What Is the Width Argument in Position_Dodge
Labeling Outliers of Boxplots in R
How to Install a Package That Has Been Archived from Cran
Converting Multiple Columns from Character to Numeric Format in R
How to Divide Each Row of a Matrix by Elements of a Vector in R
How to Calculate Cumulative Sum
Coalesce Two String Columns With Alternating Missing Values to One
How to Position Two Legends Independently in Ggplot
Using Stat_Function and Facet_Wrap Together in Ggplot2 in R
Do.Call(Rbind, List) For Uneven Number of Column
Ggplot2 Geom_Bar - How to Keep Order of Data.Frame
How to Change the Y-Axis Figures into Percentages in a Barplot