My html
<input type="datetime-local" onblur="formatDate(this.value)" />
my function
function formatDate(date) {
..
..
}
My html
<input type="datetime-local" onblur="formatDate(this.value)" />
my function
function formatDate(date) {
..
..
}
I still stand by the comments that I made below your question, however, I believe this is what you're looking for...
The code takes into account the browser's support for the datetime-local
input type and sets the output accordingly.
The output will be the one stated in your other posting of the question from this link:Formatting date value in javascript having the format:"03/02/1991 12:01 AM" and if the browser supports the datetime-local
input type, it will set the date and time accordingly. As far as I'm aware, you cannot change the format of datetime-local
to the format specified above. If you want to get the format specified above, on browsers that do support this input type, you can always create a hidden input element and set its value to the desired output for processing (as I assume this is the intention).
var support = false;
var input = document.createElement('input');
input.setAttribute('type', "datetime-local");
if (input.type !== 'text') {
support = true;
}
$("#datetime").onblur = setDateTime();
function setDateTime() {
var nDate = new Date();
var string;
var date = nDate.getDate();
var month = nDate.getMonth() + 1;
var year = nDate.getFullYear();
var hour = nDate.getHours();
var minute = nDate.getMinutes();
var second = nDate.getSeconds();
var millisecond = nDate.getMilliseconds();
var ampm = "AM";
if (date < 10) {
date = "0" + date.toString();
}
if (month < 10) {
month = "0" + month.toString();
}
if (support === false) {
if (hour === 0) {
hour = 12;
}
if (hour > 12) {
hour -= 12;
ampm = "PM";
}
}
if (hour < 10) {
hour = "0" + hour.toString();
}
if (minute < 10) {
minute = "0" + minute.toString();
}
if (support === true) {
string = year + "-" + month + "-" + date + "T" + hour + ":" + minute + ":" + second;
} else {
string = date + "/" + month + "/" + year + " " + hour + ":" + minute + " " + ampm;
}
$("#datetime").val(string);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="datetime-local" id="datetime" />