JavaScript and Dates
Tags: javascript, discuss, date, webdev
Published: 2022-04-16 21:42:36

Let's have a look at a real-world example of how to interact with dates in JavaScript. 

##### Setup
* Create a project directory and initialize your project using `npm init -y`. 
* We will be working with a created `index.js` file.
* Use 'node index.js' to run your code. 

---

##### Get present date
```Javascript
const todayDate = new Date();
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6dc3dd41yl39ago55ao.PNG)
 
---


#### Get month of the year, Year, Actual Day, Hour, Minute, milliseconds
```javascript
const todayDate = new Date();
const today = todayDate.getDate();
const month = todayDate.getMonth() + 1; //JS months starts with index 0
const year = todayDate.getFullYear();
const day = todayDate.getDay();
const hour = todayDate.getHours();
const minute = todayDate.getMinutes();
const second = todayDate.getSeconds();
const millisecond = todayDate.getMilliseconds();

console.log('Milliseconds is :', millisecond);
console.log('Seconds is :', second);
console.log('Minutes is :', minute);
console.log('Hours is :', hour);
console.log('Month is: ', month);
console.log('Year is: ', year);
console.log('Today is: ', today);
```

Result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvoesedqt4wrxd3xdurh.PNG)
 
  
---

##### Date formatting

```javascript
const todayDate = new Date();
const date = `${todayDate.getFullYear()}-${
  todayDate.getMonth() + 1
}-${todayDate.getDate()}`;

const time = `${todayDate.getHours()}:${todayDate.getMinutes()}:${todayDate.getSeconds()}`;
let dateAndTime = `${date}T${time}Z`;

console.log(date);
console.log(time);
console.log(dateAndTime);

```

Result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fusm4l5jtm00zc6qci2g.PNG)
 
---

#### Get last year and last month
```javascript
const date = new Date();
const lastMonth = new Date(date.setMonth(date.getMonth() - 1));

const dateYear = new Date();
const lastYear = new Date(dateYear.setFullYear(dateYear.getFullYear() - 1));
console.log({ lastMonth });
console.log({ lastYear });
```

---


#### Setting new date 1
```Javascript
let myDate1 = new Date('2022-04-01');
let myDate2 = new Date('2022-06-02');
console.log(myDate1);
console.log(myDate2);
```

Result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oxvk5p9c88ubsc83lfy.PNG)
 

##### Setting New Date 2
```javascript
const date = new Date().toLocaleString('en-us', {
  month: 'long',
  day: 'numeric',
  year: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: false,
});
const customDate = new Date('2022-10-10').toLocaleString('en-us', {
  month: 'long',
  day: 'numeric',
  year: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: false,
});

console.log(date);
console.log(customDate);
```

Result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y263mdb0jmo2oznvdbf6.PNG)
 
---

#### Date Methods
```javascript
let Lagos = new Date();
console.log({ toUTCString: Lagos.toUTCString() }); //Returns a date converted to UTC
console.log({ toLocaleString: Lagos.toLocaleString() }); //Converts a date to a string using the current locale
console.log({ toLocaleDateString: Lagos.toLocaleDateString() });
console.log({ toLocaleTimeString: Lagos.toLocaleTimeString() });
console.log({toLocaleString:Lagos.toLocaleString('en-US', { timeZone: 'Africa/Lagos' })});

```

Result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1qnx84tpwi8wghm3kv5.PNG)
 

---

#### Get previous date
```javascript
var date = new Date();

// date; //# => Fri Apr 01 2011 11:14:50 GMT+0200 (CEST)
console.log(date.getDate()); //"6"

date.setDate(date.getDate() - 1);
console.log(date);
```

---


##### Discuss
Please share any JavaScript date methods, functions, or logic that developers would find useful in the comments area below.


Thank you for taking the time to read this.