Two things:
1. Rather than passing just the entry.publishedDate string, as I noted
in my first post, the first argument must be a *date object* created
using the entry.publishedDate. You can do this by calling
generateRelativePubDate(new Date(entry.publishedDate),format) .
Alternatively, you could add a line at the top of the function like
this: if(typeof(theDate)=='string'){theDate=new Date(theDate)}
2. There were a number of errors in my code (that's what I get for not
actually testing before posting :) ), so here is a revised and
repaired version:
var strings={
"minutes_ago":' minutes ago',
"one_hour_ago":'1 hour ago',
"hours_ago":' hours ago'
};
var mos=[
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
function generateRelativePubDate(theDate,format){
if(typeof(theDate)=='string'){theDate=new Date(theDate)}
var now = (new Date).getTime(),
then = theDate.getTime();
if(now<then){return (2).toString()+strings['minutes_ago']}
var difference=now-then;
if(difference<3600000){
var n=Math.floor(1/60000);
return (n<=difference?2:n).toString()+strings['minutes_ago'];
}
if(difference<86400000){
var n=Math.floor(1/3600000);
if(n<=1){return strings['one_hour_ago']}
else{return v.toString()+strings['hours_ago']}
}
if(format&&format.match(/^ymd$/i)){return
[theDate
.getFullYear(),mos[theDate.getMonth()],theDate.getDate()].join(' ')}
else if(format&&format.match(/^dmy$/i)){return
[theDate
.getDate(),mos[theDate.getMonth()],theDate.getFullYear()].join(' ')}
else{return [mos[theDate.getMonth()],theDate.getDate()
+',',theDate.getFullYear()].join(' ')}
}
Jeremy R. Geerdes
Effective website design & development
Des Moines, IA
For more information or a project quote:
http://jgeerdes.home.mchsi.com
http://jgeerdes.blogspot.com
http://jgeerdes.wordpress.com
[email protected]
Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.
If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!
And check out my blog, Adventures in Web Development, at
http://jgeerdes.blogspot.com
!

On Apr 22, 2009, at 3:45 AM, sebastian de comocomo wrote:
>
> Hi Jeremy,
>
> I get this error:
>
> missing ; before statement
> [Break on this error] if(now<then){return
> 2.toString()+strings['minutes_ago']}\n
>
>
> This works:
>
> html+= i+1 + '. ' + entry.title.link(entry.link) + '<br>' +
> entry.contentSnippet + '<br>' + entry.link + '<br>' +
> entry.publishedDate
> +'<br><br>';
>
>
> I call it like this:
>
> html+= i+1 + '. ' + entry.title.link(entry.link) + '<br>' +
> entry.contentSnippet + '<br>' + entry.link + '<br>' +
> generateRelativePubDate (entry.publishedDate, ymd) +'<br><br>';
>
>
> Thanks
> Sebastian
>
>
> On 21/04/09 22:35, "Jeremy Geerdes" <[email protected]> wrote:
>
>>
>> The code for generating the relative published date is essentially
>> this:
>>
>> var strings={
>> "minutes_ago":' minutes ago',
>> "one_hour_ago":'1 hour ago',
>> "hours_ago":' hours ago'
>> };
>> var mos=[
>> 'January',
>> 'February',
>> 'March',
>> 'April',
>> 'May',
>> 'June',
>> 'July',
>> 'August',
>> 'September',
>> 'October',
>> 'November',
>> 'December'
>> ];
>> function generateRelativePubDate(theDate,format){
>> var now = (new Date).getTime(),
>> then = theDate.getTime();
>> if(now<then){return 2.toString()+strings['minutes_ago']}
>> var difference=now-this;
>> if(difference<3600000){
>> var n=Math.floor(1/60000);
>> return (n<=difference?2:n).toString()+strings['minutes_ago'];
>> }
>> if(difference<86400000){
>> var n=Math.floor(1/3600000);
>> if(n<=1){return strings['one_hour_ago']}
>> else{return v.toString()+strings['hours_ago']}
>> }
>> if(format.match(/^ymd$/i)){return join(' ',
>> [theDate.getFullYear(),mos[theDate.getMonth()],theDate.getDate()])}
>> else if(format.match(/^dmy$/i)){return join(' ',
>> [theDate.getDate(),mos[theDate.getMonth()],theDate.getFullYear()])}
>> else{return join(' ',[mos[theDate.getMonth()],theDate.getDate()
>> +',',theDate.getFullYear()])}
>> }
>>
>> The function takes two arguments. The first is a date object which
>> can
>> be obtained by calling new Date(entry.publishedDate), and the second
>> is the format you want the date to appear in. By specifying a format
>> of "ymd," whenever it returns the actual date, it'll be in the form
>> yyyy mon dd. Similarly, by specifying a format of "dmy," any full
>> date
>> string will be in the form dd mon yyyy.
>>
>> Also notice that I've separated out the key response strings, which
>> you may want to localize.
>>
>> Jeremy R. Geerdes
>> Effective website design & development
>> Des Moines, IA
>>
>> For more information or a project quote:
>> http://jgeerdes.home.mchsi.com
>> http://jgeerdes.blogspot.com
>> http://jgeerdes.wordpress.com
>> [email protected]
>>
>> Unless otherwise noted, any price quotes contained within this
>> communication are given in US dollars.
>>
>> If you're in the Des Moines, IA, area, check out Debra Heights
>> Wesleyan Church!
>>
>> And check out my blog, Adventures in Web Development, at
>> http://jgeerdes.blogspot.com
>> !
>>
>>
>> On Apr 21, 2009, at 10:21 AM, neonp wrote:
>>
>>>
>>> Hello,
>>>
>>> I am retrieving rss feeds using new google.feed.Feed(url). However,
>>> retrieving feeds like this let me no option to get a relative
>>> published date. Is there any way to reuse the google's feed API
>>> without using a FeedControl ?
>>>
>>> Thanks,
>>>
>>>>
>>
>>
>>>
>
>
>
> >


--
Hong Xiaowan 于 4/22/2009 08:23:00 下午 发布在 T 上
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google AJAX APIs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-AJAX-Search-API?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to