In order to display the date in JavaScript in yyyy-MM-dd format you need to make use of the Date object. You cannot get the date in desired string format in JavaScript as you can in Java, so you have to write few lines of code in order to get the desired date pattern.
Steps:
- Step 1: Create a Date object and store it as a constant: const today = new Date();
- Step 2: Now from the today, fetch the current day will return int value that ranges from 1-31.
- Step 3: Now from the today, fetch the current month will return int value that ranges from 0-11, 0 implies January, 11 implies December so add add 1
- Step 4: If day or month value is less than 9 prefix with zero
- Step 5: Concatenate the result with the desired hyphen
Code Example: JavaScript Date format in yyyy-MM-dd format
<div id="display-date"></div>
<script>
function getDateyyyyMMdd() {
const today = new Date();
var day = today.getDate();
var month = (today.getMonth() + 1);
if(day <=9)
day = "0"+day;
if(month <=9)
month = "0"+month;
return today.getFullYear() + '-' + month + '-' + day;
}
document.getElementById("display-date").innerHTML = getDateyyyyMMdd();
</script>
Output:

JavaScript Date format as yyyy-MM-dd
Have Questions? Post them here!
More Posts related to JavaScript,
- Meaning of javascript:void(0) explained with example
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get UTC (GMT) time using JavaScript
- Add Animated Scrolling to Html Page Title Script
- Examples: Convert String to int in JavaScript
- Get Device Screen Width and Height using javaScript
- Remove items from JavaScript array
- Javascript convert text case from uppercase to lowercase
- [javaScript] Convert text case to lowercase
- How to detect Browser and Operating System Name and Version using JavaScript
- How to send email from JavaScript HTML using mailto
- How to check if a String contains substring or a word using javaScript
- Send Extra Data with Ajax Get or Post Request
- How to get query string in JavaScript HTML location.search
- How to get current URL Location using Javascript HTML
- JavaScript: Check if variable is a number
- JavaScript : Get current page address
- JavaScript: Count Words in a String
- Loading previous page using html button using JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to Print from JavaScript HTML using window.print
- Submit html form on dropdown menu value selection or change using javascript
- Detect if Cookies are enabled using JavaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript date in yyyy-MM-dd format
More Posts:
- How to remove Siri from Menu Bar [macOS Big Sur] - MacOS
- How to fix Microsoft Windows 10 update error 80070020 - Microsoft
- Bootstrap Nav Menu Dropdown on hover - Bootstrap
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error - Android
- How to open a new tab in Notepad++ - NotepadPlusPlus
- Notepad++ Editor alternatives for Mac OS X - NotepadPlusPlus
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint
- Change Height of Android ActionBar - Android