• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get Today’s Date

Using JavaScript to Get Today’s Date

August 30, 2022 Leave a Comment

We can use JavaScript to get today’s date by making use of the JavaScript toLocaleString() method. There are many configurations we can utilize with the toLocaleString() method to display the date how we want. We will go over some of our favorites.

The JavaScript toLocaleString method will take a date object and return the date as a string using the local settings from your computer.

var currDate = new Date();
var localDate = currDate.toLocaleString();

The date is returned in a much friendlier format for the user than what the Date() method returns. The outputs of currDate and localDate from above are displayed as follows:

currDate: Fri Jan 28 2022 21:35:02 GMT-0500 (Eastern Standard Time)
localDate: 1/28/2022, 9:35:02 PM

As you can see, the day of the week is not included in the default toLocaleString output. But there are tons of options you can include as parameters to the toLocaleString method which can be found here. The following setup is one we like best:

var currDate = new Date();
var localDate2 = currDate.toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'short'});

Which would display the date and time as follows:

Tuesday, August 30, 2022 at 9:38 AM

Another option is to leave the time off of the date, and just display the day, month, and year.

var currDate = new Date();
var localDate2 = currDate.toLocaleString('en-US', { dateStyle: 'full'});

console.log(localDate2);

#Output
Tuesday, August 30, 2022

This is the preferred date format we will choose to use going forward.

Displaying the Current Date and Time using the JavaScript toLocaleString Method

Below we will provide code to get the current date in JavaScript, and let the user see it in a friendly format when they click a button.

<div id="div1">
  <div id="click-me" onclick="genNewDate()">Get Date</div>
  <p>Today's Date is: <span id="theDate"></span></p>
</div>

We will simply get the Date with with the Date() method, and use the toLocaleString() method and some parameter options to display the date and time nicely. Here is the simple JavaScript code:

function genNewDate(){
  var currDate = new Date();
  var localDate = currDate.toLocaleString('en-US', { dateStyle: 'full'});
  document.getElementById("theDate").innerHTML = localDate;
}

The final code and output for this example is below:

Code Output:

Get Date

Today’s Date is:

Full Code:

<div id="div1">
  <div id="click-me" onclick="genNewDate()">Get Date</div>
  <p>Today's Date is: <span id="theDate"></span></p>
</div>

<script>

function genNewDate(){
  var currDate = new Date();
  var localDate = currDate.toLocaleString('en-US', { dateStyle: 'full'});
  document.getElementById("theDate").innerHTML = localDate;
}

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to get today’s date.

Other Articles You'll Also Like:

  • 1.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 2.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 3.  charAt() JavaScript – Getting the Index Position of a Character
  • 4.  Using JavaScript to Remove All Non-Alphanumeric Characters From a String
  • 5.  How to Empty a String in JavaScript
  • 6.  Remove Commas From Array in JavaScript
  • 7.  Using JavaScript to Format the Date in mm dd yyyy
  • 8.  Using JavaScript to Replace Character in String
  • 9.  JavaScript acosh – Find Hyperbolic Arccosine of Number
  • 10.  How to Generate a Random Letter In JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy