I been assigned with a task where I can only use vanilla javascript. I don't even have direct access to html since they using some kind of iframe.
Scenario.
when user blur out of the input, format the date end date format - month/day/year ( 00/00/0000)
The code should format these user scenarios.
replace '-' with '/' if user uses'-' to separate date (i have)
if the user forgets to put '0' for day or month till month 9 or day 9 auto add a '0' in front of day or month or both (ex;9/9/1999 to 09/09/1999)
if the user only put 2 digits for the year, the code should assume its the year 2000's and add 20 in front of the year (ex: 9/9/15 to 09/09/2015)
how can I achieve 2, 3 using my js structure and vanilla js only? Remember i cant edit HTML so cant do like value ="this.value" , or moment js etc
function execute () {
var new_date = document.getElementById('date_val').value;
var j = format(new_date);
document.getElementById('date_val').value = j;
}
function format(incoming_date) {
var d = new Date(incoming_date);
console.log('new date', d)
day = d.getDate();
month = d.getMonth() + 1;
year = d.getFullYear();
return (String(month).length == 1 ? '0' + month : month) + '/' + (String(day).length == 1 ? '0' + day : day) + '/' + year;
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
date: <input type="text" id="date_val" onblur="execute()">
<!-- <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="./js/test.js"></script> -->
<script src="./js/test.js"></script>
</body>
</html>