...

View Full Version : "Clever" date formatting



macsperte
08-01-2008, 02:58 PM
I am looking for a script for a relative date function, something you find on every RSS feed, i.e. if the date is today is just says "Today 10:00" and for yesterday it says "Yesterday 10:00". If it's within a week it shows it like this "Thursday 10:00", to use with the news section of a clients website -

Thanks for any help!

Philip M
08-01-2008, 06:00 PM
I am afraid you do not give enough information.

How is the date generated? i.e. if today is Friday 1st what specifies that your news item was 'yesterday' (Thursday 31st).

This ought to give you a start:


<script type = "text/javascript">

function checkDate() {

var dayarray = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var end_year = 2008;
var end_month = 6;
var end_day = 26;

var d = new Date();
d.setFullYear(end_year, end_month, end_day); // YYYY,MM(0-11),DD
var selectedDate = d.getTime();

var now = new Date()
var nowtime = now.getTime();
var day = now.getDay();
var dayofweek = now.getDay();
var diff = (nowtime - selectedDate)/86400000

if (diff == 0) {y = "Today"; alert (y)}
if (diff == 1) {y = "Yesterday"; alert (y)}

if (diff > 1 && diff <=6 ) { // if date is over 6 days before show nothing
var offset = dayofweek - diff;
if (offset < 0) {offset = offset + 7}
alert (dayarray[offset]);
}
}

</script>

macsperte
08-04-2008, 02:30 PM
I am afraid you do not give enough information.

How is the date generated? i.e. if today is Friday 1st what specifies that your news item was 'yesterday' (Thursday 31st).



Thanks Philip for your reply!

Unfortunately I am a newbie concerning coding and need step-by-step instructions, in order to use a script.

However, the date is static and part of each news item, which contains the news title, the date and the news text, each in a different <p> or <span>.

I am looking for a script which transforms this date into a "clever" format. For example todays date is 8-4-2008. If there is a news item with this date I would like that visitors of the news section read it as "Today", if they are visiting the news section today. If someone reads it tomorrow, it should state "Yesterday" and for the rest of the week "Monday". Next week it should just appear as normal date. So same as RSS feeds are displaying the date.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum