brady123 Posted April 6, 2012 Share Posted April 6, 2012 I am trying to update the text in a span with a date (which is working fine), but I also want to format that date before it is updated in the span. For example, in the text area I input a date (format: YYYY-MM-DD HH:MM:SS), and I'd like to output between the span tags M-D-YYYY. Does that make sense? I'm totally lost here and hoping someone can help me out. Here is the javascript. function show_post_date() { var ele = document.getElementById("post_date"); var text = document.getElementById("new_post_date"); // Get the date value, then format it var formatted_date = ele.value; // THIS IS WHAT I'VE TRIED, BUT NOTHING IS UPDATED INTO THE SPAN IF I INCLUDE THIS var curr_date = formatted_date.getDate(); var curr_month = formatted_date.getMonth(); var curr_year = formatted_date.getFullYear(); formatted_date = curr_date + "-" + curr_month + "-" + curr_year; // END SECTION OF WHAT I'VE TRIED if(ele.value != "") { text.innerHTML = formatted_date; } } Here is the element When: <span id='new_post_date' style='font-weight:bold;'></span> <input type="text" name="post_date" id="post_date" value="" onChange='show_post_date()' /> Quote Link to comment https://forums.phpfreaks.com/topic/260462-formatting-date-before-updating-span-text/ Share on other sites More sharing options...
sunfighter Posted April 6, 2012 Share Posted April 6, 2012 Change this line var formatted_date = ele.value; to var formatted_date = new Date(ele.value); Quote Link to comment https://forums.phpfreaks.com/topic/260462-formatting-date-before-updating-span-text/#findComment-1335039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.