In order to display the date in JavaScript in yyyy-MM-dd (ISO-8601) format you need to make use of the Date object. You cannot get the date in the 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 to display date in yyyy-MM-dd format JavaScript:
- 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:

More Posts related to JavaScript,
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
More Posts:
- [fix] openssl No such file or directory error C++ - Ubuntu
- How to get list of all Java versions installed on macOS - Java
- How to start Microsoft Teams on Mac Startup - Teams
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix] - Android
- Installing Google Cloud macOS SDK - Google
- Enable Native Dark Mode in Notepad++ - NotepadPlusPlus
- Device not compatible error Android Google Play Store - Android
- Change default language highlighting in Notepad++ - NotepadPlusPlus