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,
- Get Device Screen Width and Height using javaScript
- How to Print from JavaScript HTML using window.print
- Send Extra Data with Ajax Get or Post Request
- How to get UTC (GMT) time using JavaScript
- How to detect Browser and Operating System Name and Version using JavaScript
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React
- How to send email from JavaScript HTML using mailto
- Remove items from JavaScript array
- JavaScript : Get current page address
- Add Animated Scrolling to Html Page Title Script
- [javaScript] Convert text case to lowercase
- JavaScript date in yyyy-MM-dd format
- Power of Print Statements in JavaScript: A Comprehensive Guide
- Submit html form on dropdown menu value selection or change using javascript
- Write javaScript code in Swedish using FikaScript
- Javascript convert text case from uppercase to lowercase
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- JavaScript: Count Words in a String
- Meaning of javascript:void(0) explained with example
- JavaScript: Generate Random Numbers between 1 and 3
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to get current URL Location using Javascript HTML
- Fix: SyntaxError: The requested module does not provide an export named default
- Fix: Error: Cannot find module /node-examples/init
- JavaScript: Check if variable is a number
More Posts:
- How to Change Android Title Bar Color? - Android
- Notepad++ display files on tab bar as horizontal instead of vertical - NotepadPlusPlus
- Cannot access Windows application shortcuts on Start menu and Taskbar - Windows
- [Mac] Find a file using filename in macOS Terminal - MacOS
- Can we move apps like WhatsApp, Facebook to external MicroSD card - WhatsApp
- How to Configure Mac with PPPoE Ethernet if using DSL or Cable Modem - MacOS
- Fix SharePoint Error - Unable to Display Named Item - SharePoint
- RabbitMQ Queue Listener Java Spring Boot Code Example - Java