Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Location
    Hamburg, Germany
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    "Clever" date formatting

    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!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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:

    Code:
    <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>
    Last edited by Philip M; 08-01-2008 at 06:53 PM.

  • Users who have thanked Philip M for this post:

    macsperte (08-04-2008)

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Location
    Hamburg, Germany
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •