Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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!

— asked May 5th 2010 by Jeffrey Messick
  • date
  • datejs
  • formatting
0 Comments

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 :)

    — answered May 5th 2010 by Dan Tamas
    permalink
    0 Comments
  • 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];

    — answered May 5th 2010 by Jeffrey Messick
    permalink
    0 Comments
  • you need to use

    title:'Release Date ' +new_date;
    

    and of course to make the format change before the creation of the label

    — answered May 5th 2010 by Dan Tamas
    permalink
    0 Comments
  • 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];

    — answered May 5th 2010 by Jeffrey Messick
    permalink
    0 Comments
  • THANK YOU!!!!!! It worked. I guess when you have been coding for hours you over look things! Thanks again Mate!

    — answered May 5th 2010 by Jeffrey Messick
    permalink
    0 Comments
  • 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.

    — answered August 18th 2011 by Michael Stelly
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.