• 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 New Date Format in dd mm yy

Using JavaScript to Get New Date Format in dd mm yy

September 1, 2022 Leave a Comment

We can use JavaScript to get a new date in the format of dd mm yy 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. One of these configurations will get the format we want.

Note that we will use the toLocaleDateString() method is our examples below. This method works the same way as the toLocaleString() method, except that it will just display the date, and NOT the time.

Here is the code to get the date in the format of dd-mm-yy. We will explain how we got this below.

var currDate = new Date();
var localDate = currDate.toLocaleDateString('es-CL', {year: '2-digit', month: '2-digit', day: '2-digit'});

console.log(localDate);

#Output
01-09-22

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

So if we wanted to display the date in the dd-mm-yy format, we can use different options for parameters in the toLocaleString() method. A list of these options can be found here.

So to get the date in the format of dd-mm-yy, we can simply use the es-CL language format as the only parameter in the toLocaleString() method. Let’s see this below.

var currDate = new Date();
var localDate1 = currDate.toLocaleDateString('es-CL', {year: '2-digit', month: '2-digit', day: '2-digit'});

console.log(localDate1);

#Output
01-09-22

If we want to display the date with slashes instead of hyphens, dd/mm/yy, we can use the following parameter:

var currDate = new Date();
var localDate3 = currDate.toLocaleDateString('en-AU', {year: '2-digit', month: '2-digit', day: '2-digit'});

console.log(localDate3);

#Output
01/09/22

There are other languages we can put as the parameter that would also display the date in the dd-mm-yy format as well, we just chose the first one we saw down the list.

Another very simple way to display the date in the dd-mm-yy format is to simply get the year, month, and date in the format we want, and combine them. Here is some code below to show how easily this can be done.

var currDate = new Date();
var year = currDate.toLocaleString('en-US', { year: '2-digit'});
var month = currDate.toLocaleString('en-US', { month: '2-digit'});
var day = currDate.toLocaleString('en-US', { day: '2-digit'});

var dateInDDMMYY = day + "-" + month + "-" + year;

console.log(dateInDDMMYY);

#Output
01-09-22

Displaying the Current Date and Time in DD-MM-YY 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 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.toLocaleDateString('es-CL', {year: '2-digit', month: '2-digit', day: '2-digit'});
  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.toLocaleDateString('es-CL', {year: '2-digit', month: '2-digit', day: '2-digit'});
  document.getElementById("theDate").innerHTML = localDate;
}

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to get a new date in the format of dd mm yy.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Add Leading Zeros
  • 2.  Using JavaScript to Square a Number
  • 3.  Add Years to a Date Using JavaScript
  • 4.  JavaScript if else Shorthand Statement
  • 5.  Using JavaScript to Calculate Average of Array of Numbers
  • 6.  innerHTML vs outerHTML
  • 7.  Using JavaScript to Convert Float to Integer
  • 8.  Using JavaScript to Set the Width of a Div
  • 9.  Using JavaScript to Count Even Numbers in an Array
  • 10.  Using Javascript to Remove Class from Element

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