1). Is there a way to do this manually with an algorithm or maths formula.
2). if not then any function in JavaScript ?
please try to answer Q.1.
ex.
UNIX Time : 1637785944 converted to : GMT: Wednesday, 24 November 2021 20:32:24 conversion done using online converter.
Thank You!
CodePudding user response:
Hi and welcome to stackoverflow.
Heres a javascript function that works perfectly for my epoc unix timestamps.
This will format it in to a javascript date.
function epochToDate(date) {
var utcSeconds = date;
var d = new Date(0);
d.setUTCSeconds(utcSeconds);
return new Date(d)
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
See the update for an answer to the question OP is actually asking
You should use standard JavaScript Date object multiplying the timestamp by 1000 to convert from seconds to milliseconds since 12am on 1st January 1970. This will get you a Date object that represents this moment in time. There are some subtleties around your local timezone that can be ignored by using toUTCString
or toGMTString
const timestamp = 1637785944;
const date = new Date(timestamp * 1000);
console.log(`local timezone: ${date.toString()}`);
console.log(`GMT string: ${date.toGMTString()}`);
console.log(`UTC string: ${date.toUTCString()}`);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
UPDATE
Although I have already answered your question as stated, it appears that you're after something different and the timestamps you have have been offset for some timezone other than UTC/GMT.
The best way to fix this would be to fix the source of data so that timestamps correctly represent a number as seconds since midnight on 1st January 1970.
From the page you linked to in your comment (epochconvertor.com):
What is epoch time?
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
What you have is not a true Unix timestamp and is asking for trouble as you have discovered.
Having said all that, what can we do?
If you know that all timestamps are recorded in a specific timezone you could manually adjust the timestamp by adding/subtracting a predetermined value from the timestamp - your example uses Los Angeles with an offset of 28800 seconds.
Unfortunately for you, a timestamp has no notion of an associated timezone so either:
- You have a fixed offset for all timestamps
- You record either the offset or the timezone (from which you can figure out the offset)
const timestamp = 1637768249; // LA time
const timezoneOffset = 28800;
const date = new Date((timestamp - timezoneOffset)* 1000);
console.log(`local timezone: ${date.toString()}`);
console.log(`GMT string: ${date.toGMTString()}`);
console.log(`UTC string: ${date.toUTCString()}`);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Question - do your timestamps track daylight savings or not?
If so, the offset will need to track the same changes, otherwise you're going to have to deal with working out whether an additional offset should be calculated based on the date which will be difficult when clocks are adjusted back 1 hour and the same timestamp could represent 2 different times an hour apart. Here, in the UK, the clocks go back at 2am BST to 1am GMT meaning that there are, in effect 2 instances of 1:30 am on the day the clocks go back.
All of this complication is caused by not recording the timestamp as UTC. This is the same reason that any half-decent developer will insist that dates and times are recorded/stored as UTC/GMT, because it's trivial to convert from UTC/GMT to any other timezone.