Unix Timestamp Help

Unix Timestamp Help
0
#1

So wanted to try to convert Unix Time Stamps not be using keywords or shortcuts. I wanted to have multiple loops that subtract from the millisecond (year worth), then add to, for instance, a year variable.

   while (date > 31557600) {
        date -= 31557600;
        year++;
    }

Here is the full code --> https://repl.it/@John_Nicole/LinedObesePinkriverdolphin

Note that i convert it to seconds at line 10.

Everything works fine but the hours. I thought Unix started at 12:00 pm on January 1st, 1970.

    var year = 1970;
    var month = 1;
    var day = 1;
    var hour = 0;
    var minute = 0;
    var second = 0;

Instead of '12:45.40' I get '17:45.40'.

I have no idea why, 60^2 = 3600 seconds in a hour.

0 Likes

#2

Midnight (00:00:00), not 12:00 pm.

1 Like

#3

Even then it reads 17:52.57

5 hours off…

0 Likes

#4

Are you accounting for your time zone? Are you by any chance 5 hours off of UTC?

0 Likes

#5

Im not sure, anyway to check? I used new Date().getTime()

I know new Date() already gives you exactly what my output is, but why wouldn’t it be my local time?

0 Likes

#6

By default that will give you the time in UTC. If you’re in the US on the East Coast, then your local time is 5 hours behind UTC.

0 Likes

#7

Oh okay, so whats the point of it then? Is this just for comparing between your time and UTC?

0 Likes

#8

It gives a location-agnostic datetime. You can have it convert either to the client’s local time (toLocaleString I think) or to a specific timezone (maybe using the same function, but with a parameter. I don’t recall off the cuff).

0 Likes

#9

Okay, thanks


0 Likes