Date to readable format
In my script I have a xhr.xml query that gets a release date in the following format yyyy/mm/dd. I place this variable into a label to display the release date. I would like to have it in the following format: MM/dd/yyyy.
I have tried to convert it using several scripts that are out on the internet, but most of them that I have found only return the current date. I tried to use date.js file that is on google with no luck.
All I need it to do is change the format to a more readable format. Does anyone have an example js script on how to do this or know of a site that offers an example?
Many thanks!
6 Answers
-
Accepted Answer
date = "2010/23/04"; var date_arr = date.split("/"); var new_date = date_arr[1]+"/"+date_arr[2]+"/"+date_arr[0];
Ugly but simple :)
-
So i set my variable as released that is where i get the released date from the xml file:
var released = item.getElementsByTagName("released").item(0).text;
in the label I use title:'Release Date ' +released; // displays as 2009-12-18 needs to be 12-18-2010
Tried this below doesn't work: I also changed the variable in the label to title:date;
date = released; // from xml file displays as 2009-12-18 needs to be 12-18-2010
var date_arr = date.split("-");
var new_date = date_arr[1]+"-"+date_arr[2]+"-"+date_arr[0]; -
you need to use
title:'Release Date ' +new_date;
and of course to make the format change before the creation of the label
-
So i set my variable as released that is where i get the released date from the xml file:
var released = item.getElementsByTagName("released").item(0).text;
in the label I use title:'Release Date ' +released; // displays as 2009-12-18 needs to be 12-18-2010
Tried this below doesn't work: I also changed the variable in the label to title:date;
date = released; // from xml file displays as 2009-12-18 needs to be 12-18-2010
var date_arr = date.split("-");
var new_date = date_arr[1]+"-"+date_arr[2]+"-"+date_arr[0]; -
THANK YOU!!!!!! It worked. I guess when you have been coding for hours you over look things! Thanks again Mate!
-
I had a slightly different issue. I needed to parse a date parameter. Borrowing from Dan, I made a function like this:
function formatDate(e){ var dd = e.getDate(); var mm = e.getMonth() + 1; var yyyy = e.getFullYear(); var formattedDate = mm + '/' + dd + '/' + yyyy; return formattedDate; }
and I implemented it like this:
//UPDATE VALUE IF CHANGED datePicker.addEventListener('change', function() { txtDOB.value = formatDate(datePicker.value); });
Hope that helps others.