javascript

Javascript Get Date Methods You Can Quickly Use

In Javascript, dates are objects and are inbuilt. There are also methods that can be used to access these date objects, especially when you want to implement these methods in your software using Javascript.

Here are nine (9) Javascript Get date methods you can quickly use:

1. GET DATE

Get date as a number (1-31):

let date = new Date().getDate()
//21

2. GET TIME

Get current time (in milliseconds since January 1, 1970)

let time = new Date().getTime();

3. GET HOURS

Get current hour (0-23)

let hours = new Date().getHours();

4. GET MINUTES

Get the minute (0-59)

let minutes = new Date().getMinutes()

5. GET SECONDS

Returns the number of seconds from (0-59)

let seconds = new Date().getSeconds()

6. GET DAY

Returns the weekday as a number (0-6)

let day = new Date().getDay();
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

console.log(days[day]);

7. GET MONTH

Returns the month as a number (0-11)

let month = new Date().getMonth() + 1;
//To get the accurate month we must increment by 1

8. GET FULL YEAR

Returns the year as a four-digit number (yyyy)

let year = new Date().getFullYear();
//2021

9. DATE NOW

Returns the number of milliseconds that has elapsed since January 1, 1970 00:00:00 UTC

let date = new Date()
let now = date.now();

Conclusion

These are the 9 common Javascript Get Date Methods that you can reference in your projects.

See also:

7 Javascript Shortcuts You Should Use in Your Next Project

Recent Posts

Why parseInt(’09’) Returns 0

If you've ever encountered the puzzling behavior of parseInt('09') returning 0 in JavaScript, you're not…

2 days ago

Event Bubbling and Capturing: Why Your Click Listener Fires Twice (And How to Fix It)

If you’ve ever built an interactive web application, you may have encountered a puzzling issue:…

1 week ago

Practical Array Methods for Everyday Coding

Arrays are the backbone of programming, used in nearly every application. Whether you're manipulating data,…

2 weeks ago

What the Heck Is the Event Loop? (Explained With Pizza Shop Analogies)

If you've ever tried to learn JavaScript, you’ve probably heard about the "Event Loop"—that mysterious,…

2 weeks ago

Why [] === [] Returns false in JavaScript (And How to Properly Compare Arrays & Objects)

JavaScript can sometimes behave in unexpected ways, especially when comparing arrays and objects. If you've…

2 weeks ago

Recursion for Beginners

Recursion is a programming technique where a function calls itself to solve smaller instances of…

2 weeks ago