• 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
  • VBA
  • About
You are here: Home / JavaScript / JavaScript toLocaleString – Display the Date and Time Using JavaScript

JavaScript toLocaleString – Display the Date and Time Using JavaScript

January 28, 2022 Leave a Comment

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 localDate2 = currDate.toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'short'});

Which would display the date and time as follows:

Friday, January 28, 2022 at 10:14 PM

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', timeStyle: 'short'});
  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', timeStyle: 'short'});
  document.getElementById("theDate").innerHTML = localDate;
}

</script>

Hopefully this article has been useful in helping you understand the JavaScript toLocaleString method.

Other Articles You'll Also Like:

  • 1.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 2.  Using JavaScript to Round to 2 Decimal Places
  • 3.  Insert Character Into String In JavaScript
  • 4.  Using JavaScript to Set Visibility
  • 5.  How to Iterate through an Array Using JavaScript
  • 6.  Using JavaScript to Get Image Dimensions
  • 7.  Examples Using the JavaScript += Addition Assignment Operator
  • 8.  JavaScript isPrime – How to Check if a Number is Prime in JavaScript
  • 9.  Check if Checkbox is Checked in JavaScript
  • 10.  Using JavaScript to Reverse a Number

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy