How to convert GMT date time into GMT Unix TimeStamp using JS?
Without a timezone, new Date assumes localtime
so, lets say you retrieve the string
var date = '2018-08-21 11:37:56';
what you can do is
var date = '2018-08-21 11:37:56';var t = new Date(date.replace(' ', 'T') + 'Z').getTime() / 1000;console.log(t); // 1534851476 - see for yourself
Getting date in GMT from unix timestamp
This is the problem, conceptually:
//date obtained here is in IST on my system which needs to be converted into GMT.
Date time = new Date(Long.valueOf(unixTimeStamp) * 1000);
A Date
doesn't have a time zone. This is the value you want. The fact that when you call toString()
it converts it to your local time zone is irrelevant to the value that it's actually representing. A Date
is just a number of milliseconds since the Unix epoch (1st January 1970, midnight UTC). So your whole method can be:
private static Date convertToDate(String unixTimeStamp)
{
return new Date(Long.valueOf(unixTimeStamp) * 1000);
}
You don't need any kind of formatter, as you're not really trying to get a textual representation.
I would advise you to use Joda Time for date/time work if you can, by the way - it's a much cleaner API.
UNIX timestamp always in GMT?
yep, UNIX timestamp represents how much seconds past from unix-time epoch in GMT+0
How to get GMT date from Unix epoch time milliseconds?
Change calendar.setTimeInMillis(time / 1000)
to calendar.setTimeInMillis(time * 1000)
The number of milliseconds is 1000 times the number of seconds; not 1/1000 the number.
How to convert timestamp from GMT to Unix epoch
SimpleDateFormat
is outdated, as Gael pointed out.
The Time API now supports up to nanoseconds, so microseconds are not an issue here. You should use DateTimeFormatter with ZonedDateTime
. Your pattern is slightly wrong. Checking the docs for Offset Z
:
Offset Z: This formats the offset based on the number of pattern
letters. One, two or three letters outputs the hour and minute,
without a colon, such as '+0130'. The output will be '+0000' when the
offset is zero. Four letters outputs the full form of localized
offset, equivalent to four letters of Offset-O. The output will be the
corresponding localized offset text if the offset is zero. Five
letters outputs the hour, minute, with optional second if non-zero,
with colon. It outputs 'Z' if the offset is zero. Six or more letters
throws IllegalArgumentException.
You can also print the time using toInstant
to make sure it was parsed correctly:
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
val parsed = "2022-01-12T17:17:34.512492+0000"
val p = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZ"
val dtf = DateTimeFormatter.ofPattern(p)
val zdt = ZonedDateTime.parse(parsed, dtf)
println(zdt.toInstant) // 2022-01-12T17:17:34.512492Z
println(zdt.toInstant.toEpochMilli) // 1642004254512
Here is a nice article that explains in detail converting an ISO 8601 in Java. The comments at the end are particularly useful, as it shows the difference between the different patterns used.
How to convert GMT date to UNIX time in golang?
The layout of the date is RFC1123
. Parse it with time.Parse()
then convert it to Unix with time.Time.Unix()
.
t, _ := time.Parse(time.RFC1123, "Mon, 23 Dec 2019 18:52:45 GMT")
tUnix:= t.Unix()
fmt.Printf("%s in Unix is %d",t,tUnix)
// Output: 2019-12-23 18:52:45 +0000 GMT in Unix is 1577127165
Try it online
How to change timezone from GMT+0 to GMT+2 of this Unix epoch time (Milliseconds) conversion Query?
I found a way to do this and it works nicely for my specific needs. Here is my Query;
LEFT(DATEADD(SECOND, FLOOR([epochtime_column]/1000 ), '1970-01-01') AT TIME ZONE 'UTC' at TIME ZONE 'Central European Standard Time', 19)
You basically chop off the 3 added numbers from the epoch time in milliseconds to turn it into epoch time in seconds. After which you add those seconds up to 1970-01-01 to turn it into a readable date.
This is only a viable strategy when you don't need the milliseconds from the epoch time. IF you do need the milliseconds then this solution will not work for you.
Related Topics
Reusing Custom Makefile for Static Library with Cmake
Why Kernel Needs Virtual Addressing
Identify Program That Connects to a Unix Domain Socket
Docker Run Hello-World Still Fails, Permission Denied
How to Allow Jenkins to Access The Files That Only Root or Some Specific Programs Have Access To
Difference Between a Stripped Binary and a Non Stripped Binary in Linux
Why Is The Close Function Is Called Release in 'struct File_Operations' in The Linux Kernel
How to Manually Install The Eclipse-Cdt Plugin from an Archive/Zip on Ubuntu
How to Find The Reason for a Dead Process Without Log File on Unix
How to Block Push to Master Branch on Remote
Linux Kernel: Kernel Version String Appended with Either ''+" or "-Dirty"
How to Route Tcp/Ip Responses Through a Different Interface