How Can I Collect Form Input In Date Format And Display It On Page
Solution 1:
For solve your problem you can use this code source :
<!DOCTYPE html><html><body><h1>JavaScript Can Validate Input</h1><p>Please input a date with this template: 17/06/1982</p><inputid="cInDate"><buttontype="button"onclick="myFunction()">Submit</button><pid="dateI"></p><script>functionmyFunction() {
var date = document.getElementById("cInDate").value;
var dateArray = date.split("/");
var s = document.getElementById("dateI");
s.textContent = dateArray[1]+"th of July";
}
</script></body></html>
Solution 2:
If input type date is supported, it returns a value in ISO 8601 format yyyy-mm-dd. Note that there is no time zone associated with it. If parsed by the Date constructor, most modern browsers will return a Date for date based on midnight, UTC.
If the host is in a timezone west of Greenwich, its local date values be offset for the time zone so the date will be for the previous day. If you want the date treated as local, you must manually parse it:
/* Parse date string in ISO 8601 format as local
** @param {string} s - Date string like 2016-04-01
** @returns {Date} If values are invalid, returns an invalid Date
*/functionparseISODate(s) {
var b = s.split(/\D/);
var d = newDate(b[0], b[1]? b[1] - 1 : 0, b[2] || 1);
return d && d.getMonth() == b[1] - 1? d : newDate(NaN);
}
<formonsubmit="
this.dateString.value = parseISODate(this.inDate.value);
return false;
"><inputtype="date"name="inDate"value="2016-07-18"><br><inputtype="text"name="dateString"size="70"><br><button>Convert date</button></form>
However, not all browsers in use support input type date, so you should deal with that.
Solution 3:
The errors were being caused by your comments not being formatted properly. <!-- content -->
define an HTML comment, but doesn't work in Javascript. For JS, use //Content
or /* Content */
.
Also be careful about your JS syntax. Note the +
I added in the last line of your function, as well as the closing </input>
and</p>
tags in your HTML. *As RobG has noted this isn't strictly necessary, but it helps me at least.
//this is a javascript comment
checkin = function(){
var date = document.getElementById("cInDate").value;
var dateArray = date.split("-");
console.log(dateArray);
var s = document.getElementById("dateI");
s.textContent = dateArray[1]+"th of July";
}
<formid="myForm">
Check In Date: <inputtype="date"id="cInDate"><buttononclick="checkin()">Submit</button></form><h1class="al">Hotel Room Availability</h1><pclass="al">You want to check in on
<spanid="dateI"></span></p><!-- shows checkin date -->
Edit: You also need to change .split('/')
to .split('-')
, because as you can see from the console, the date is separated by -
s
Post a Comment for "How Can I Collect Form Input In Date Format And Display It On Page"