'Is there a way to auto-format a date in DD/MM/YYYY (with the /) in pure JS?
This is an input[type=text] that auto-format as a date. It automatically adds the / between the day, month, and year. The problem is that I'm trying to render this in "DD/MM/YYYY" format instead of "MM/DD/YYYY", but can't figure out how.
var date = document.getElementById("date");
function checkValue(str, max) {
if (str.charAt(0) !== "0" || str == "00") {
var num = parseInt(str);
if (isNaN(num) || num <= 0 || num > max) num = 1;
str =
num > parseInt(max.toString().charAt(0)) && num.toString().length == 1
? "0" + num
: num.toString();
}
return str;
}
date.addEventListener("input", function (e) {
this.type = "text";
var input = this.value;
if (/\D\/$/.test(input)) input = input.substr(0, input.length - 3);
var values = input.split("/").map(function (v) {
return v.replace(/\D/g, "");
});
if (values[0]) values[0] = checkValue(values[0], 12);
if (values[1]) values[1] = checkValue(values[1], 31);
var output = values.map(function (v, i) {
return v.length == 2 && i < 2 ? v + " / " : v;
});
this.value = output.join("").substr(0, 14);
});
date.addEventListener("blur", function (e) {
this.type = "text";
var input = this.value;
var values = input.split("/").map(function (v, i) {
return v.replace(/\D/g, "");
});
var output = "";
if (values.length == 3) {
var year =
values[2].length !== 4 ? parseInt(values[2]) + 2000 : parseInt(values[2]);
var month = parseInt(values[0]) - 1;
var day = parseInt(values[1]);
var d = new Date(year, month, day);
if (!isNaN(d)) {
document.getElementById("result").innerText = d.toString();
var dates = [d.getDate(), d.getMonth() + 1, d.getFullYear()];
output = dates
.map(function (v) {
v = v.toString();
return v.length == 1 ? "0" + v : v;
})
.join(" / ");
}
}
this.value = output;
});
<input type="text" placeholder="MM/JJ/AAAA" id="date"/>
EDIT: I'm using NuxtJS, so if you know of anything that would allow me to achieve the same thing, and still allow me to use my own styles, that would be nice.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
