JavaScript Date Get and Set Methods
Tags: node, webdev, javascript, tutorial
Published: 2021-02-16 20:26:37

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

---

### Date *get* methods
To get information from a date object, the `get` method is required.

let assume `const d = new Date()`.

See the table below:

|   Method    |Description   |    Example   |
|------------|-----------------|------------|
|`getFullYear` |Get full year (yyyy) |`d.getFullYear()` |
|`getMonth` |Get month (0 - 11) |`d.getMonth()` |
|`getDate` |Get date (1 - 31) |`d.getDate()` |
|`getHours` |Get hours (0 - 23) |`d.getHours()` |
|`getMinutes` |Get minutes (0 - 59) |`d.getMinutes()` |
|`getSeconds` |Get seconds (0 - 59) |`d.getSeconds()`|
|`getMilliseconds` |Get milliseconds (0 - 999) |`d.getMilliseconds()`|
|`getTime` |Get time (>= 1613560292960) |`d.getTime()`|
|`getDay` |Get day (0-6) |`d.getDay()`|
|`Date.now` |Get current year day in milliseconds (0-6) |`Date.now()`|

> `getMonth()`: `0` is January and `11` is February. 

> `getDay()`: `0` is Sunday and `6` is Saturday.

> The table may also include the **Universal Time Coordinated** (**UTC**) date. For example `d.getUTCDay()`.

See the examples below:

```js
const d = new Date();
const months = [
  "Jan", "Feb", "March", "April", "May", "June", 
  "July", "Aug", "Sep", "Oct", "Nov", "Dec"
  ];

months[d.getMonth()];
```

See another example below:

```js
const d = new Date();
const days = ["Sun", "Mon", "Tues", "Thur", "Fri", "Sat"];
days[d.getDay()];
```

### *Parse*
The `parse` method allows you to parse a data string format to get the timestamp in milliseconds from 1 January 1970 till the time specified in the string format.

The syntax is shown below:

```
Date.parse(str)
```

See the example below:

```js
const parseTime = Date.parse('2055-01-22T10:48:13.201-06:00');

console.log(parseTime); // 2684249293201
``` 

---

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

---

### Date *set* methods

To set information from a date object, the `set` method is required.

let assume `const d = new Date()`.

See the table below:

|   Method    |Description   |    Example   |
|------------|-----------------|------------|
|`setFullYear` |Set full year (yyyy) |`d.setFullYear(...)` |
|`setMonth` |Set month (0 - 11) |`d.setMonth(...)` |
|`setDate` |Set date (1 - 31) |`d.setDate(...)` |
|`setHours` |Set hours (0 - 23) |`d.setHours(...)` |
|`setMinutes` |Set minutes (0 - 59) |`d.setMinutes(...)` |
|`setSeconds` |Set seconds (0 - 59) |`d.setSeconds(...)`|
|`setMilliseconds` |Set milliseconds (0 - 999) |`d.setMilliseconds(...)`|
|`setTime` |Set time (>= 1613560292960) |`d.setTime(...)`|
|`setDay` |Set day (0-6) |`d.setDay(...)`|

> The table may also include the **Universal Time Coordinated** (**UTC**) date. For example `d.setUTCDay()`.

See the examples below:

```js
const d = new Date();
d.setFullYear(2069);
d; // 2069-mm-ddThrs:mins:secs.msecsZ
```

It is optional to include Month and day.

```js
const d = new Date();
d.setFullYear(2069, 03, 20);
d; // 2069-04-20Thrs:mins:secs.msecsZ
```

### Mixed *set* and *get* methods
It is possible to use both methods together.

See the example below:

```js
const d = new Date();
d.setDate(d.getDate() + 30);
d;
```

The addition above is handled automatically by the Date object by shifting months or year.

**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" />