Mastering JavaScript: How to Get the Current Date & Time

Getting the current date and time in JavaScript is easy. You can generate the current date and time using the inbuilt `Date` function automatically. It gives you the current date and time based on your computer or your device setting.

Basics – JavaScript Date Object

  Mastering JavaScript: How to Get the Current Date & Time  1   First understand the basic syntax by which we can generate the current date and time.   Mastering JavaScript: How to Get the Current Date & Time  2   Create a variable. Use the `new` keyword and then the `Date` function. This `Date` function is inbuilt in JavaScript, so you do not need to define it on your own. You simply need to use it.   Mastering JavaScript: How to Get the Current Date & Time  3   Print the variable to see the output.   Mastering JavaScript: How to Get the Current Date & Time  4  
const now = new Date();
console.log(now);
  Mastering JavaScript: How to Get the Current Date & Time  5   You will see detailed information about the time and the date. It first shows the week day and then the month, the date, the year, the time, and the timezone. By this you can get the whole information about the time and the date.   Mastering JavaScript: How to Get the Current Date & Time  6   Read More: break and continue

Get only the date – JavaScript Date Object

  Mastering JavaScript: How to Get the Current Date & Time  7   If you want to get only the current date because the full output is long, use `toDateString()` on the `Date` object. After calling the variable, put a dot and write `toDateString()`. You are separating out the date string from the variable which is holding the whole date information.   Mastering JavaScript: How to Get the Current Date & Time  9  
const now = new Date();
console.log(now.toDateString());
// Example output: "Tue Aug 20 2024"
  Mastering JavaScript: How to Get the Current Date & Time  8   You will get the week day, the month, the date, and the year. This is the information about the date, not the time.   Mastering JavaScript: How to Get the Current Date & Time  10   Read More: typeof examples

Get only the time – JavaScript Date Object

  Mastering JavaScript: How to Get the Current Date & Time  11   If you want to get only the current time, put a dot after the variable name and write `toTimeString()`. For generating the date we were using `toDateString()` and for generating the time we are using `toTimeString()`.   Mastering JavaScript: How to Get the Current Date & Time  13  
const now = new Date();
console.log(now.toTimeString());
// Example output: "14:37:52 GMT+0530 (India Standard Time)"
  Mastering JavaScript: How to Get the Current Date & Time  12   You will get the time and the timezone. This is how you generate your time only.   Mastering JavaScript: How to Get the Current Date & Time  14   Read More: JS prototypes

Extract parts – JavaScript Date Object

  Mastering JavaScript: How to Get the Current Date & Time  15   There are a lot more methods that you can use with the `Date` object to get specific parts. For getting the year we use `getFullYear()`. For getting the month we use `getMonth() + 1` because the month is zero based. For getting the date of the month we use `getDate()`. For getting the hour we use `getHours()`. For getting minutes we use `getMinutes()`. For seconds we use `getSeconds()`.   Mastering JavaScript: How to Get the Current Date & Time  16  
const now = new Date();

console.log("Year:", now.getFullYear());
console.log("Month:", now.getMonth() + 1); // zero based, add 1
console.log("Date:", now.getDate());
console.log("Hours:", now.getHours());
console.log("Minutes:", now.getMinutes());
console.log("Seconds:", now.getSeconds());
  Mastering JavaScript: How to Get the Current Date & Time  17   `toDateString()` was generating the date string which was holding all the information like the month and the week day, but for getting only the numeric date we use the `getDate()` function. By using the `Date` object with these methods, you can get all the information separately with simple code.   Mastering JavaScript: How to Get the Current Date & Time  18  

Final thoughts

Use `new Date()` to get the current date and time, `toDateString()` for the date only, `toTimeString()` for the time only, and methods like `getFullYear()`, `getMonth() + 1`, `getDate()`, `getHours()`, `getMinutes()`, and `getSeconds()` to access parts individually. This covers the essentials you need to work with the JavaScript Date Object effectively.

Leave a Comment