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:
![JavaScript Date format as yyyy-MM-dd JavaScript Date format as yyyy-MM-dd](https://code2care.org/javascript/javascript-date-yyyy-mm-dd-format/images/JavaScript Date format as yyyy-MM-dd.png)
More Posts related to JavaScript,
- How to Run JavaScript on Mac Terminal
- Get Current time in GMT/UTC using JavaScript
- How to yarn reinstall all Packages
- [javaScript] Convert text case to lowercase
- Get Device Screen Width and Height using javaScript
- Fix - npm start: sh: index.js: command not found
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- JavaScript: Convert an Image into Base64 String
- JavaScript : Get current page address
- How to get query string in JavaScript HTML location.search
- Create React App using npm command with TypeScript
- JavaScript: Count Words in a String
- Add Animated Scrolling to Html Page Title Script
- How to send email from JavaScript HTML using mailto
- Javascript convert text case from uppercase to lowercase
- Submit html form on dropdown menu value selection or change using javascript
- Send Extra Data with Ajax Get or Post Request
- Fix: SyntaxError: The requested module does not provide an export named default
- Examples: Convert String to int in JavaScript
- 10 ways to Convert String to a Number in JavaScript
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- Fix: ReferenceError: require is not defined in ES module scope [Node]
- [JavaScript] Remove all Newlines From String
- How to detect Browser and Operating System Name and Version using JavaScript
More Posts:
- How to Convert a Notepad++ file to an Excel File - NotepadPlusPlus
- Clearing the Cache in Firefox Web Browser - HowTos
- How to mute all sounds in Notepad++ - NotepadPlusPlus
- Java Thread.sleep() Method Deep Dive with Examples - Java
- Fix - zsh: command not found: conda [macOS] - zsh
- How to create an Optional from existing Object in Java? - Java
- [macOS] Change homepage Macbook Safari Browser - MacOS
- How to Open Private Browsing (Incognito Mode) in Safari on Mac - MacOS