# Unix Timestamp Help

**URL:** <https://forum.freecodecamp.org/t/unix-timestamp-help/168889>\
**Category:** General\
**Created:** [January 15, 2018, 5:49pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889 "2018-01-15T17:49:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [January 15, 2018, 5:49pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/1 "2018-01-15T17:49:16Z")

</div>

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.

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

```

Here is the full code --\> [https://repl.it/@John\_Nicole/LinedObesePinkriverdolphin](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.`

```auto
    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.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [January 15, 2018, 5:52pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/2 "2018-01-15T17:52:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [January 15, 2018, 5:53pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/3 "2018-01-15T17:53:24Z")

</div>

Even then it reads `17:52.57`

5 hours off…

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [January 15, 2018, 5:56pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/4 "2018-01-15T17:56:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [January 15, 2018, 5:59pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/5 "2018-01-15T17:59:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [January 15, 2018, 6:01pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/6 "2018-01-15T18:01:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [January 15, 2018, 6:06pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/7 "2018-01-15T18:06:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [January 15, 2018, 6:09pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/8 "2018-01-15T18:09:00Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![John-freeCodeCamp](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@John-freeCodeCamp](https://forum.freecodecamp.org/u/John-freeCodeCamp)\
**Post date:** [January 15, 2018, 6:35pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/9 "2018-01-15T18:35:50Z")

</div>

Okay, thanks

* * *

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 6:03pm UTC](https://forum.freecodecamp.org/t/unix-timestamp-help/168889/10 "2021-01-16T18:03:08Z")

</div>


