Date & Time in JavaScript
Tags: javascript, beginners, computerscience, webdev
Published: 2021-01-24 13:07:39

Let’s meet someone amazing today 😁, haha a new built-in object: Date. In Javascript, we can easily work with Date & Time using the Date class. To create an object of this type we use the _new_ keyword.

```javascript
 // Creating a Date object
    var dateObj = new Date();

    console.log(dateObj);
```
By doing this, we create an object with the current date and time formed by weekday, month, day, year, hours, minutes, seconds, and timezone. Now that's friggin precise 😝.

###Methods of the Date object	😵
|Method	            |Description                                 |
|-------------------|--------------------------------------------|
|getDate()	    |Returns the day of the month (1 to 31)      |
|getDay()	    |Returns the weekday (0 - Sunday to 6 -      |
|                   |Saturday)                                   |
|getFullYear()	    |Returns the full year (YYYY)                |
|getMonth()	    |Returns the month (0 to 11)                 |
|getHours()	    |Returns the hours (0 to 23)                 |
|getMinutes()	    |Return the minutes (0 to 59)                |
|getSeconds()	    |Returns the seconds (0 to 59)               |
|getMilliseconds()  |Returns the milliseconds (0 to 999)         |
|getTime()	    |Returns the number of milliseconds since the|
|                   | Epoch(Jan 1st, 1970, 00:00:00)             |
|setTime()	    |Creates a specific date from milliseconds   |
|                   |since the epoch.                            |

#####What is UNIX EPOCH?😰

The Epoch is present in most programming languages. We can consider it the starting point of counting the time. This is useful to do calculations with date and time.

Javascript uses the Unix Epoch, which is: Jan 1st, 1970, 00:00:00, UTC.

###A Time Chart for reference 	😅

|Time	            |Milliseconds (ms)|
|-------------------|-----------------|
|1 second	    |1,000            |
|1 minute	    |60,000           |
|1 hour	            |3,600,000        |
|1 day	            |86,400,000       |
|1 year (365 days)  |31,536,000,000   |

#####How to calculate how many hours have passed since the Epoch?	😬
```javascript
    var Obj = new Date();

    Obj = Obj.getTime();

    var hours = Obj / 3600000;

    console.log(Math.floor(hours));
```
###Passing Argument to __Date()__  	😷
We created date objects without passing any arguments, this is why they represented the current time but we can also use them with specific objects or better to say a specific number of objects.

|Numeric arguments | Time represents                          |
|------------------|------------------------------------------|
|One               |milliseconds since the epoch              |
|Two               | years and month (months go from 0 to 11) |
|Three             | years, month, and day                    |
|Four              | years, month, day, and hours             |
|Five              | years, month, days, hours, minutes       |
|Six               | years, month, day, hours, minutes, and   |                    |                  |seconds                                   |

```javascript
var Obj = new Date(2021,1,24);
    console.log( Obj );
```
We can also pass strings as an argument and in different formats
<ul>
<li>ISO date: "2020-03-18" (YYYY-MM-DD)</li>
<li>Short Date: "03/18/2020" (MM/DD/YYYY)</li>
<li>Long Date: "Mar 18 2020" or "18 Mar 2020"</li>
</ul>

I hope you guys have learnt and saved it for later reference
and if you found my grammatical mistakes then don't forget to comment them 😂