How can I modify a raw Unix timestamp so that it shows that it is 5 hours behind (as an example). I'm looking to do this with a javascript or python. The more lightweight the better. I'm basically looking for how to manually decode a given unix timestamp and change some of its numbers so that it gives me back a unix timestamp showing a different time. It would be even greater if I could automatically adjust it to a users personal time-zone using javascript/python.
2 Answers
Convert to the number of hours you want to offset by to seconds, and then add or subtract it from the Unix timestamp. As far as getting the user's personal time zone, I'm not sure how you would do that without language specific code.

- 1,427
- 1
- 9
- 16
How to choose a time 5 hours numerically smaller
This would be relevant for if, for example, you were testing whether the submission of an exam answer is within 5 hours of the exam's start time.
For this, just subtract 5 * 3600 * 1000 from the Unix timestamp's numerical value.
What you are actually proposing to do is extremely unwise
You seem to be planning to create a Unix timestamp of a different point in time which, when expressed as UTC but with the annotation "UTC" deleted, will match the local time display expected by a user who is 5 hours behind UTC. I can see why you are tempted to do this but it is a very bad idea.
Unix Timestamps do not default to be in UTC, they describe a point in time across all of space simultaneously. If you shift the value of a Unix timestamp, it is no longer a Unix timestamp, just as
(mass of car minus 50 kg)
is no longer the mass of that car. The value is either the mass of a different car that is 50kg lighter, or an incorrect value for the mass of the original car.Unix timestamps are unambiguous. Once you know that a variable contains a Unix timestamp, you can stop worrying about any if's, but's or maybe's. It is solid and definite. What you are creating is a horrible thing which looks like a Unix timestamp of an timepoint, but it is not. What variable name are you going to give it to prevent confusion? You might give the physical property a new name, such as the
goalieTimeStamp
, which is distinguished from Unix timestamps by being displaced by 5 hours.If a person is 5 hours behind UTC now (in January), that person will likely be a different number of hours behind UTC in summertime. This is a mess.
I think you are doing this so that you can display a local time nicely. Choose a different, better, way to achieve this.
You should use the localisation system in the relevant language to obtain and display the local time, which will depend not only on the location of the user, but also the time of year. This will also allow you to deal with languages etc, if you need to.
And throughout your code you will have a clear distinction between the timepoint of your event (invariant across space) and how a local user will express that time in their timezone, time of year and language.
A good library for this in Javascript is moment.js
. It is rather heavyweight, but this is because the task is much more heavyweight that it first seems!

- 8,816
- 1
- 17
- 26