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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / JavaScript / getDate JavaScript – Get the Current Day of the Month in JavaScript

getDate JavaScript – Get the Current Day of the Month in JavaScript

January 16, 2022 Leave a Comment

The getDate JavaScript method will get the current day of the month from a date object using the getDate() method. It will return a number from 1 to 31.

var currDate = new Date();
var currDay = currDate.getDate();

The value of currDay in the code above would return whatever the day of the month it currently is. The date this post was written was January 16th, so 16 would be the value of currDay.

Getting and Displaying the Current Day, Month, and Year with the Help of the getDate method in JavaScript

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 first get the current date using new Date(). Once we have the date object, we can then get the day using the getDate() method, the year using the getFullYear() method, and the month using the getMonth() method.

We can make the date look even better by converting the month number into the month as a String. We just need to make an array with all the months to make this happen, which we will do below.

function genNewDate(){
  var currDate = new Date();
  var currDay = currDate.getDate();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthName = months[currMonth];
  document.getElementById("theDate").innerHTML = monthName + ' ' + currDay + ', ' + currYear;
}

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 currDay = currDate.getDate();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthName = months[currMonth];
  document.getElementById("theDate").innerHTML = monthName + ' ' + currDay + ', ' + currYear;
}

</script>

Hopefully this article has been useful in helping you understand the getDate Javascript method.

Other Articles You'll Also Like:

  • 1.  Remove Braces from String Using JavaScript
  • 2.  JavaScript Array includes Method
  • 3.  How to Remove Decimals of a Number in JavaScript
  • 4.  Using JavaScript to Check if Variable Exists
  • 5.  Set Text with the textContent Property in JavaScript
  • 6.  How to Remove All Numbers From String in JavaScript
  • 7.  Using JavaScript to Detect Window Resize
  • 8.  Using JavaScript to Check if Number is Divisible by Another Number
  • 9.  How to Exit for Loop in JavaScript
  • 10.  Remove All Instances of Value From an Array 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