DEV Community

Larry
Larry

Posted on

1 2

use javascript Date()

Alt Text

How do we get current date using Date() function?

var date = new Date();
console.log("Today's date is:", date);

// Today's date is: Tue Mar 23 2021 23:14:47 GMT+0600 (Bangladesh Standard Time)
Enter fullscreen mode Exit fullscreen mode

Here output will give with GMT and timezone.
If we want to grab specific value then we need to use some method like getMonth(), getDate(), getFullYear() etc...

console.log("Today's date is " + date.getMonth() + "/" + date.getDate() + "/" + date.getFullYear());
// Today's date is 2/23/2021
Enter fullscreen mode Exit fullscreen mode

Here, getMonth() return month index and it is start with 0 that why month says 2 instead of 3 . So, if we get correct month, we have to add 1 with the method.
getDate() return current date.
getFullYear() return 4 digits of current year.

console.log("Today's date is " + (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear());
// Today's date is 3/23/2021
Enter fullscreen mode Exit fullscreen mode

Thanks

Image of Bright Data

Access Niche Markets with Ease – Unlock restricted market data with precision.

Get access to hard-to-reach data with our specialized proxy services designed for niche markets.

Access Markets

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay