-4

http://jsfiddle.net/58gw8sr7/

My html

<input type="datetime-local" onblur="formatDate(this.value)" />

my function

    function formatDate(date) {
..
..
}
4
  • Your fiddle loads it in the window load event. Also, paragraphs don't have a value attribute, use innerHTML instead. Commented Jan 3, 2015 at 4:27
  • 2
    formatDate is defined in the load event handler, so it's not global. But inline event handlers can only call global functions. So get rid of inline event handlers and use addEventListener Commented Jan 3, 2015 at 4:27
  • 2
    This is an exact duplicate... stackoverflow.com/questions/27751096/… and then stackoverflow.com/questions/27751236/… I'm not sure if it's the same person, but this is the 3rd of this exact question today, all within the last 2 hours or so Commented Jan 3, 2015 at 4:33
  • @AaronSwartz Try reformulating your question properly, look at this: stackoverflow.com/help/how-to-ask, don't repeat your question multiple times in the forums, it will get marked as a duplicate. Do your research first. Commented Jan 3, 2015 at 4:41

1 Answer 1

1

I still stand by the comments that I made below your question, however, I believe this is what you're looking for...

jsfiddle

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" />

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.