PublishedDate

74 views
Skip to first unread message

Sean Mitchell

unread,
Jun 11, 2013, 9:55:43 AM6/11/13
to google-ajax...@googlegroups.com
Hi

I am using google RSS feed to pull in an xml file, this works well, however the date is reversed. In the XML file, the date is correct (DAY-MONTH-YEAR)  but in my app the dates (Months and Days) are reversed. Is this easy to fix? and if so, could anybody please help me?

Any help would be greatly appreciated? 

Below is the code that i have so far:
var googleResult  = [];
    google.load("feeds", "1");
    function initialize() {
   
   feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
   feed.includeHistoricalEntries();
   feed.setNumEntries(90);
   feed.load(function (result) {
   console.log(result);

    if (!result.error) {
    googleResult  = result ;
    var output = '<ul data-role="listview" data-filter="true"  data-filter-placeholder="Search for a phrase/word">';
    var container = document.getElementById("feed");
    for (var i = 0; i < result.feed.entries.length; i++) {
    var entry = result.feed.entries[i];
var publishedDate = new Date($(this).find("publishedDate").text());
var day = publishedDate.getDate();
var month = publishedDate.getMonth() + 1;
var year = publishedDate.getFullYear();
var SMdate = month + '/' + day + '/' + year;
output += '<li>';
    output += '<a href= "#feedContent?i=' + i + '" data-transition = "slide" >';
    //output += '<p class="ui-li-aside">' + entry.publishedDate + '</p>';
output += '<h1>' + entry.title + '</h1>';
output += '<p>' + entry.publishedDate + '</p>';
//output += '<p class="ui-li-aside">' + entry.publishedDate.slice(0, 12) + '</p>';
var snippet = entry.contentSnippet;
//var code = entry.content.slice(-3);
var sm = entry.contentSnippet;
var code = entry.content.match(/\/VS|\/VN|\/PM\/OT/);
//var code = entry.content.match(/VNT|VT\d\d/);
var nCode = sm.concat(code);
//alert(code);
    //output += '<p>' + entry.contentSnippet + '</p>';
output += '<p>' + nCode + '</p>';
    output += '</a>';
    output += '</li>';
    }
    output += '</ul>';
    $('#jobsFeed').html(output);
    $('#jobsFeed').trigger('create');

    }
   });
    }
    google.setOnLoadCallback(initialize);

    $(document).on("pagebeforechange", function (e, data) {
     // We only want to handle changePage() calls where the caller is
     // asking us to load a page by URL
     if (typeof data.toPage === "string") {
     // We are being asked to load a page by URL
     var u = $.mobile.path.parseUrl(data.toPage), _re = "#feedContent";

     if (u.hash.search(_re) !== -1) {
     var i = urlParam("i", data.toPage);
     $("#feedContent").remove();
     var $page = $("<div data-role='page' id='feedContent' data-add-back-btn='true'><div data-role='header'><h1>" + googleResult.feed.entries[i].title + "</h1></div></div>");
     var $content = $("<div data-role='content' id='feedContent'></div>");
$content.append(googleResult.feed.entries[i].content);
     $page.append($content);
     $.mobile.pageContainer.append($page); 
     }
     }
    });

Jeremy Geerdes

unread,
Jun 11, 2013, 5:53:26 PM6/11/13
to google-ajax...@googlegroups.com
Probable the easiest would be to locate the line as below

output += '<p>' + entry.publishedDate + '</p>';

and replace it with something like this

output += '<p>' + publishedDate.getDate() + ' ' + ['Jan','Feb','Mar','Apr'...][publishedDate.getMonth()] + ' ' + publishedDate.getFullYear() + '</p>';

Hope that helps.


--
--
You received this message because you are subscribed to the Google
Groups "Google AJAX APIs" group.
To post to this group, send email to
google-ajax...@googlegroups.com
To unsubscribe from this group, send email to
google-ajax-searc...@googlegroups.com
To view this message on the web, visit
https://groups.google.com/d/msgid/google-ajax-search-api/9457f65e-12b3-48be-bd10-c524569f2ff9%40googlegroups.com?hl=en_US
For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

Sean Mitchell

unread,
Jun 12, 2013, 4:54:34 AM6/12/13
to google-ajax...@googlegroups.com
Hi

Thanks for your reply, i have changed the code but unfortunately it wont work at all, it breaks the code. I had changed the month bit where you placed ... and there was no syntax errors. If i console.log the text, it says that the date is invalid, and the text that displays on my app is 1 Jan, 1970! arhh!

The date seems to be wrong on import, if i console log the data, it displays the date in full like Wednesday 6 November, 2013 0.00-8:00 etc, but if i dive deep into the xml structure I can find the unmodified date such as 11/06/2013.

This is the path
entries[0].xmlnode.childnodes.26: pubdate.firstChild:text.data

Can I access that path, and if so how can i grab it, if not, is there a better way?!

Thanks again for all your help so far


Jeremy Geerdes

unread,
Jun 12, 2013, 7:52:57 AM6/12/13
to google-ajax...@googlegroups.com
Do you have a URL where we can see this code in action? It's a whole lot easier to debug if we can see what's going on in a browser.

jg





For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sean Mitchell

unread,
Jun 12, 2013, 8:48:48 AM6/12/13
to google-ajax...@googlegroups.com
Hi, yea sure. I have hosted it here: http://animalhealthadvisor.co.uk/VOL/VT/jobsFeed.html
The page in question is the search tab, the date is on each listings.
If it helps, i could supply you with a zip file with the contents?

Thanks again for your helps so far.

Cheers

Sean


Jeremy Geerdes

unread,
Jun 12, 2013, 9:57:46 AM6/12/13
to google-ajax...@googlegroups.com
Well, a couple of things. First, the code on your page differs from the code that you supplied. On the line I offered, you need to alter the three instances of publishedDate to publishedDateSM. Second, the line starting

var publishedDateSM = new Date(...

apparently doesn't work. JQuery returns an empty string for the query you're trying to use to initiate the date object. So replace that line with this:

var publishedDateSM = new Date(entry.publishedDate);

See how that works.

jg





For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sean Mitchell

unread,
Jun 12, 2013, 10:21:12 AM6/12/13
to google-ajax...@googlegroups.com
Hi,

Sorry about the confusion in versions, i have been trying things myself but to no success.

I have replaced this to: 

output += '<p>' + publishedDateSM.getDate() + ' ' + ['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct', 'Nov','Dec'][publishedDateSM.getMonth()] + ' ' + publishedDateSM.getFullYear() + '</p>'; 

and replaced 

var publishedDateSM = new Date(entry.publishedDate);

This is now much clearer, and tidy's up the date by removing all unwanted text such as the time, but it still the wrong way around. I have updated the link, and you should be able to see it, where the first listing is 6 Dec 2013, that should be 12 June! The xml file, that i am streaming it from is correct :-(

Cheers

Sean



Jeremy Geerdes

unread,
Jun 12, 2013, 10:40:40 AM6/12/13
to google-ajax...@googlegroups.com
That's because the pubDate element in the feed is not formatted correctly. It should be in RFC-822, taking the following form:

Wed, 02 Oct 2002 08:00:00 EST

or

Wed, 02 Oct 2002 15:00:00 +0200

The API backend is apparently trying to parse the invalid date, but it's assuming that the feed is in the traditional (American) format of MM/DD/YYYY rather than DD/MM/YYYY

If you don't have control over the feed's format, you'll have to implement a workaround in your script. Something like the following would probably work:

REMOVE the line reading

var publishedDateSM = new Date(entry.publishedDate);

REPLACE it with the following code:

var dateEls = entry.xmlNode.getElementsByTagName('pubDate')[0].firstChild.data.match(/(\d+)/g);
var publishedDateSM = new Date(parseInt(dateEls[2]), parseInt(dateEls[1])-1, parseInt(dateEls[0]));


jg




For more options, visit this group at
http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-ajax-searc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sean Mitchell

unread,
Jun 12, 2013, 11:00:43 AM6/12/13
to google-ajax...@googlegroups.com
You are an absolute genius  Thank you, thank you very much. I knew it would be down to the way the feed was set up, as its been a nightmare from the start, and they have gave me no help what-so-ever. My app now is as good as finished apart from the small matter of push notifications!

Thanks again, its greatly appreciated.

Thank you :-)



Reply all
Reply to author
Forward
0 new messages