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);
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.
Convert timestamp to Date time for a particular timezone
You cannot convert a timestamp to another timezone, cause timestamps are always GMT, they are a given moment in the line of time in the universe.
We humans are used to local time on our planet, so a timestamp can be formatted to be more human readable, and in that context it is converted to a local timezone.
Using legacy java.util.* packages, this is done as follows:
DateFormat tzFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
tzFormat.setTimeZone(TimeZone.getTimeZone("CET")); // Use whatever timezone
System.out.println(tzFormat.format(date));
If you need to make "math" over the timestamp on local timezone (like, tomorrow at 8:00 local timezone), then the situation is more complex.
To do this you can resort to a number of hacks (like parsing or modifying the string obtained with the method above), or use the new Java date & time classes that have a specific class to deal with date and time in local time zones:
Instant timestamp = Instant.ofEpochMilli(inputValue);
ZonedDateTime romeTime = timestamp.atZone(ZoneId.of("Europe/Rome"));
Note how this second example uses "Europe/Rome" and not generically "CET". This is very important if you're planning to deal with timezones where DST is used, cause the DST change day (or if they use DST or not) may change from country to country even if they are in the same timezone.
convert datetime to timestamp
Python does not fully support the ISO 8601 standard. You should use dateutil.parser instead:
>>> from dateutil.parser import isoparse
>>> isoparse("2020-01-01T01:39:40.000000Z")
datetime.datetime(2020, 1, 1, 1, 39, 40, tzinfo=tzutc())
How do I convert timestamp to datetime format in python for cosmos data?
Use pd.to_datetime
with unit="s"
:
df["Date"] = pd.to_datetime(df["_ts"], unit="s")
print(df)
Prints:
_ts etag Date
0 1646255207 xyz 2022-03-02 21:06:47
1 1646257427 abc 2022-03-02 21:43:47
2 1646297798 None 2022-03-03 08:56:38
3 1646333451 dfg 2022-03-03 18:50:51
And to get last 24h data:
print(df[df["Date"] > df["Date"].max() - pd.Timedelta(hours=24)])
How to convert grpc java proto timestamp to date?
Assuming you are referring to google.protobuf.Timestamp
, the easiest way to convert is with the com.google.protobuf.util.Timestamps utility:
Timestamp timestamp = Timestamp.fromMillis(date.getTime());
Timestamp stores the date as seconds and nanoseconds since 1970 whereas Date stores milliseconds since 1970. If you consult the google.protobuf.Timestamp
documentation, it mentions how to do this manually conversion:
// The example used currentTimeMillis(), but let's use Date instead.
// long millis = System.currentTimeMillis();
long millis = date.getTime();
Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
.setNanos((int) ((millis % 1000) * 1000000)).build();
Related Topics
Stop Scheduled Timer When Shutdown Tomcat
Using an Instance of an Object as a Key in Hashmap, and Then Access It with Exactly New Object
Write a Program That Will Surely Go into Deadlock
How to Create an 2D Arraylist in Java
Resize a Picture to Fit a Jlabel
Refreshing Static Content with Spring MVC and Boot
Pass Data from Java Servlet to Jsp
Maven. Lambda Expressions Are Not Supported in -Source 1.5
Logging User Activity in Web App
How to Call Super Constructor in Lombok
Write a File in Utf-8 Using Filewriter (Java)
In Java, How to Define an Integer Constant in Binary Format
Maven "Build Path Specifies Execution Environment J2Se-1.5", Even Though I Changed It to 1.7
How Do Java Method Annotations Work in Conjunction with Method Overriding
Differencebetween Canonical Name, Simple Name and Class Name in Java Class