Jump to content

Formatting date before updating span text?


brady123

Recommended Posts

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()' />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.