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 Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs