JavaScript Date Object
Tags: webdev, javascript, tutorial, node
Published: 2021-02-15 21:18:31

[![host.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613285550606/kVnFkQSP1.png)](https://partners.hostgator.com/MaZ1N)

---
JavaScript is an **object-oriented language**; everything is an object, including data that aren't, can be modeled as objects.

The general syntax is:

```
obj = new Object([...]) // instantiation
```

`[...]` = optional parameters

> Instantiation is the process of creating a new object.

Objects can be a `String` `Date` `Audio` `Array`, etc.

See the examples below:

```js
const strObj = new String("Bello");
console.log(strObj, typeof strObj); // [String: 'Bello'] object

const arrObj = new Array(27, "Bello", true, 7n)
console.log(arrObj, typeof arrObj); // [ 27, 'Bello', true, 7n ] object

const dateObj = new Date();
console.log(dateObj, typeof dateObj); 
// year-month-dayThours:minutes.millisecondsz object

/* 
const audioObj = new Audio(url);
console.log(audioObj, typeof audioObj); 
*/
``` 

In this article, the `Date` object will be the case study.

---

### Creating *new Date* Objects
`new Date()` constructor is used to create a date object.

Syntax:

```
dateObj = new Date([...])
```

where `[... ]` = optional arguments

The syntax above is in 4 ways:

```
new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)
```

---
### *new Date()*
The object, `new Date()` represents the current date and time.

```js
const dateObj = new Date();
dateObj; // year-month-dayThours:minutes.millisecondsz
```

To convert the above date to milliseconds (since 1 January 1970), the `getTime()` method is needed.

```js
const dateObj = new Date();
const toMsecs = dateObj.getTime();

console.log(`The current time in Milliseconds is ${toMsecs}ms`);
```

The example above is the same as below:

```js
const dateObj = new Date();

console.log(`The current time in Milliseconds is ${+dateObj}ms`);
```

More on **get methods** in the next article

The date format above is in **ISO** standard format. 

Where `T` = Time; `Z` = UTC

The object is a **static date object**. To make the object tick (dynamic), Web **APIs** (**Application Programming Interface**) are needed. 

> To get the date in milliseconds from the beginning of the current year use `Date.now()`.

```js
const toMsec = Date.now();
console.log(toMsec);
```

---
[![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613214987327/HP8_p_PnK.png)](https://domain.mno8.net/c/2653712/848392/9560)

---
### *new Date(year, month, ...)*
Date and time specification has the syntax below:

```js
date = new Date(year, month, day, hours, minutes, seconds, milliseconds)
```

See the example below:

```js
const dateObj = new Date(2057, 11, 03, 13, 34, 41, 992);
dateObj; // year-month-dayThours:minutes.millisecondsz
```

---
[![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613214824530/xhsMHPv2n.png)](https://domain.mno8.net/c/2653712/848392/9560)

---
> Months are counted from 0 to 11. January is 0. December is 11.

The minimum number of arguments is 2, the month is required. 

The order of the arguments is compulsory.

```js
const dateObj1 = new Date(2057, 11);
// const dateObj1 = new Date(2057-11);
// const dateObj1 = new Date(2057/11);
// const dateObj1 = new Date("2057 Nov");
dateObj1;

const dateObj2 = new Date(2057, 11, 3);
// const dateObj2 = new Date(2057-11-3);
// const dateObj2 = new Date(2057/11/3);
// const dateObj2 = new Date("2057 Nov 3");
dateObj2;

const dateObj3 = new Date(2057, 11, 3, 13);
// const dateObj3 = new Date("2057 Jan 3 13:00:00");
// const dateObj3 = new Date("2057 Jan 3 13:00:00.000");
dateObj3;
```

If the `date` object is single, the output is `1970-01-01T00:00:02.057Z`

```js
const dateObj = new Date(2057);
dateObj; // 1970-01-01T00:00:02.057Z
```

JavaScript interprets the single argument, `2057` as `02` in seconds, `057` in milliseconds since January 01, 1970.

---
### *new Date(milliseconds)*
`new Date(milliseconds)` creates a new date object as zero time plus milliseconds.

See the example below:

```js
const prevCentury = new Date(0);
prevCentury; // 1970-01-01T00:00:00.000Z
```

The time (T) `1970-01-01` is equivalent to `January 01, 1970 00:00:00`

> One day (24 hours) is 86,400,000 milliseconds.

```js
const dateObj = new Date(86_400_000);
dateObj; // 1970-01-02T00:00:00.000Z
```

---
### *new Date(dateString)*
`new Date(dateString)` creates a new date object from a date string:

```js
const prevCentury = new Date("January 01, 1970 00:00:00");
// const prevCentury = new Date("1970 January 01 00:00:00");
prevCentury; // 1970-01-01T00:00:00.000Z

const nextCentury = new Date("November 03, 2077 12:59:10.453");
// const nextCentury = new Date("2077 November 03 12:59:10.453");
nextCentury; // 2077-11-03T12:59:10.453Z
```

> The comma `,` in the example above is optional but more readable

---
### Dates Conversion 
In the table below, let `const d = new Date()`

See the date conversion table below:

| Methods        | Conversions           | Examples  |
| ------------- |---------------|-------|
| `toString`      | converts to a string | `d.toString()` |
| `toUTCString`     | converts to a UTC string     |  `d.toUTCString()` |
| `toDateString` | converts to a more readable format      |   `d.toDateString()` |
| `toISOString` | converts to a string, using the ISO standard format      |   `d.toISOString()` |
| `toJSON` | converts to JSON format      |   `d.toJSON()` |
| `toLocaleDateString` | converts using local conventions      |   `d.toLocaleDateString()` |
| `toLocaleTimeString` | converts using local conventions      |   `d.toLocaleTimeString()` |
| `toLocaleString` | converts using local conventions      |   `d.toLocaleString()` |
| `toTimeString` | converts to string      |   `d.toTimeString()` |

Check out the complete list of methods on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#static_methods)

**Happy coding!!!**

---

[![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613216267892/cgCrviTiF.png)](https://www.buymeacoffee.com/techstackmedia)

---

<a href="https://skillshare.eqcm.net/c/2680282/300216/4650" target="_top" id="300216"><img src="//a.impactradius-go.com/display-ad/4650-300216" border="0" alt="Learn on Skillshare" width="300" height="250"/></a><img height="0" width="0" src="https://skillshare.eqcm.net/i/2680282/300216/4650" style="position:absolute;visibility:hidden;" border="0" />