---
title: Timestamps
author: Patrick Dwyer
description: This article explains how to work with timestamps in JavaScript.
silly data: will this work?
published: 2025-1-22
---


# Working with Timestamps in JavaScript

**Here are a few sources that can help** [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp)

In JavaScript, **timestamps** represent the number of milliseconds that have elapsed since **January 1, 1970, 00:00:00 UTC** (known as the **Unix Epoch**)... which is kind of cool but also a major pain in the ass. Timestamps are probably the least bad way of working with dates and times.

This article will explain how to work with timestamps in JavaScript, covering the following:

1. How to get the current timestamp.
2. How to convert timestamps to something useful.
3. How to manipulate timestamps.

## 1. Getting the Current Timestamp

To get the current timestamp, you can use the `Date.now()` method, which returns the timestamp in milliseconds which is super useful if you're a machine.

It is also possible to create a `Date` object and use its `getTime()` method. ---------add more on this later

### Code Examples

```html
  <h4>
    Put this in html body and press a key to show current timestamp
  </h4>
  <p>timeStamp: <span id="time">-</span></p>
```

```js
// add this to in script
function getTime(event) {
    const time = document.getElementById("time");
    time.firstChild.nodeValue = event.timeStamp;
}
```

When we get a keypress for Date.now the time of the cl

```js
// Using Date.now()
const currentTimestamp = Date.now();
console.log('Current Timestamp:', currentTimestamp); // Example: 1587360000000

// Date().getTime() example here

```

```html
document.addEventListener('keypress', getTime);
```

## Making it readable for us humans

By using the `Date` object in JavaScript we can make things understandable, as in not miliseconds.

Lets take the example timestamp from above and create a ``new Date()``, assign it a variable, and format it in to a string using ``toLocalString()``.  

### Code Example:

```js
const timestamp = 1587360000000; // Example timestamp

const date = new Date(timestamp);
console.log('Converted Date:', date.toLocaleString()); 
```

## Modifing Timestamps

Timestamps can be modified, unless they are converted to a string value, like any other number.

### Code Example

```js
const timestamp = Date.now(); // for current timestamp
console.log('Current Timestamp:', timestamp);

const oneDayMilliseconds = 24 * 60 * 60 * 1000; // 1 day in milliseconds
const newTimestamp = timestamp + oneDayMilliseconds; // Add 1 day

const newDate = new Date(newTimestamp);
console.log('New Date After Adding One Day:', newDate.toLocaleString());
// Example output: "04/21/2020, 04:20:00 AM"

```
