Google Feed API change date format

Discussion in 'JavaScript' started by patrick0001, Jul 3, 2012.

  1. #1
    For the highlight code, how can I change the date format e.g. 12-Jul-2012 instead of 12/7/12

    Problem page: gen-education.com/index3.html

    The following code I get the documentation from http://www.javascriptkit.com/dhtmltutors/googleajaxfeed2.shtml

    google.load("feeds", "1") //Load Google Ajax Feed API (version 1)
    
    function rssdisplayer(divid, url, feedlimit, showoptions){
    this.showoptions=showoptions || "" //get string of options to show ("date" and/or "description")
    var feedpointer=new google.feeds.Feed(url) //create new instance of Google Ajax Feed API
    feedpointer.setNumEntries(feedlimit) //set number of items to display
    document.write('<div id="'+divid+'">Loading feed...</div>')
    this.feedcontainer=document.getElementById(divid)
    var displayer=this
    feedpointer.load(function(r){displayer.formatoutput(r)}) //call Feed.load() to retrieve and output RSS feed
    }
    
    
    [B]rssdisplayer.prototype.formatdate=function(datestr){
    var itemdate=new Date(datestr)
    //var itemdate=' '+itemdate.getFullYear()+"/"+(itemdate.getMonth()+1)+"/"+itemdate.getDate()
    var itemdate=itemdate.getDate()+"/"+(itemdate.getMonth()+1)+"/"+itemdate.getFullYear()
    return "<span style='color:#ED1C24; font-size: 90%; font-weight:bold;'>"+itemdate.toLocaleString()+"</span>"
    }[/B]
    
    
    rssdisplayer.prototype.formatoutput=function(result){
    if (!result.error){ //if RSS feed successfully fetched
    var thefeeds=result.feed.entries //get all feed entries as a JSON array
    var rssoutput="<ul>"
    for (var i=0; i<thefeeds.length; i++){ //loop through entries
    //var itemtitle="<a href=\"" + thefeeds[i].link + "\">" + thefeeds[i].title + "</a>"
    var itemtitle=thefeeds[i].title
    var itemdate=/date/i.test(this.showoptions)? this.formatdate(thefeeds[i].publishedDate) : ""
    
    //var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet  : ""
    //rssoutput+="<li>" + itemtitle + " " + itemdate + itemdescription + "</li>"
    var itemdescription=/description/i.test(this.showoptions)? "<br />"+thefeeds[i].content : /snippet/i.test(this.showoptions)? "<br />"+thefeeds[i].contentSnippet  : ""
    rssoutput+="<li>" + itemdate + "<br />" + itemtitle + "</li>"
    }
    rssoutput+="</ul>"
    this.feedcontainer.innerHTML=rssoutput
    }
    else //else, output error
    alert("Error fetching feeds: "+result.error.message)
    }
    
    //USAGE SYNTAX: new rssdisplayer("divid", "rssurl", numberofitems, "displayoptions")
    //new rssdisplayer("adiv", "http://www.cssdrive.com/index.php/news/rss_2.0/", 5, "date, description")
    Code (markup):
    Another date problem:
    Problem: gen-education.com/events2.html

    How can I change dialog pop up date format from Wed Jul 04 2012 11:48:31 GMT+0800 (Malay Peninsula Standard Time) to 04 Jul 2012
     
    patrick0001, Jul 3, 2012 IP
  2. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #2
    There are no. of javascript date formatting libraries like Date.js, xDate etc. Also jQuery has an inbuilt date formatter.
    But i think you can do here with a small change to your code.You can use an array like,
    
    var month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    
    Code (markup):
    You can then replace the following line
    
    var itemdate=itemdate.getDate()+"/"+(itemdate.getMonth()+1)+"/"+itemdate.getFullYear()
    
    Code (markup):
    with this line
    
    var itemdate=itemdate.getDate()+"-"+month_names[itemdate.getMonth()]+"-"+itemdate.getFullYear()
    
    Code (markup):
     
    Unni krishnan, Jul 3, 2012 IP