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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Converting/adding to date format

    Hello,

    I have an event list that's been pulled in dynamically, that displays event dates/times like this -

    17th September 10pm - 3am

    Code:
    <dd class="msl_event_time">17th September 10pm - 3am</dd>
    There are multiple events, but all with the same class name.

    All I would like to do, is prepend the day of the week before the date, so the above example would become 'Tuesday 17th September 10pm - 3am'.

    Is there a way I can do this with Javascript?

    Thanks, Rich.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,071
    Thanks
    38
    Thanked 498 Times in 492 Posts
    You can do this with JS, but how do you plan on handling events that cross over to a different year than the current one?

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    You can do this with JS, but how do you plan on handling events that cross over to a different year than the current one?
    Thanks for the reply, all of the events are within a set timeframe (13th September 2013 - 21st September 2013) so that wouldn't be an issue.

    I have thought of my own solution, which works, but I'm sure there's a more efficient way of doing it -

    $('.msl_event_time:contains("14th")').prepend('Saturday ');
    $('.msl_event_time:contains("15th")').prepend('Sunday ');
    $('.msl_event_time:contains("16th")').prepend('Monday ');
    $('.msl_event_time:contains("17th")').prepend('Tuesday ');
    $('.msl_event_time:contains("18th")').prepend('Wednesday ');
    $('.msl_event_time:contains("19th")').prepend('Thursday ');
    $('.msl_event_time:contains("20th")').prepend('Friday ');
    $('.msl_event_time:contains("21st")').prepend('Saturday ');

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    
    <dd class="msl_event_time">17th September 10pm - 3am</dd>
    <dd class="msl_event_time">18th September 10pm - 3am</dd>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Format(cls){
     var months=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
     var days=['Sunday','Monday','Tuesday','Wednsday','Thursday','Friday','Saturday']
     var objs=document.body.getElementsByTagName('DD'),txt,d,m,y=new Date().getFullYear(),z0=0,z0a;
     for (;z0<objs.length;z0++){
      if ((' '+objs[z0].className+' ').match(' '+cls+' ')){
       txt=objs[z0].innerHTML;
       d=txt.split(/\D/)[0];
       m=txt.split(' ')[1].slice(0,3).toLowerCase();
       for (z0a=0;z0a<12;z0a++){
        if (m==months[z0a]){
         objs[z0].innerHTML=days[new Date(y,z0a,d).getDay()]+' '+objs[z0].innerHTML;
         break;
        }
       }
      }
     }
    }
    
    Format('msl_event_time');
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    swiltch (07-28-2013)

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vwphillips View Post
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    
    <dd class="msl_event_time">17th September 10pm - 3am</dd>
    <dd class="msl_event_time">18th September 10pm - 3am</dd>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Format(cls){
     var months=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
     var days=['Sunday','Monday','Tuesday','Wednsday','Thursday','Friday','Saturday']
     var objs=document.body.getElementsByTagName('DD'),txt,d,m,y=new Date().getFullYear(),z0=0,z0a;
     for (;z0<objs.length;z0++){
      if ((' '+objs[z0].className+' ').match(' '+cls+' ')){
       txt=objs[z0].innerHTML;
       d=txt.split(/\D/)[0];
       m=txt.split(' ')[1].slice(0,3).toLowerCase();
       for (z0a=0;z0a<12;z0a++){
        if (m==months[z0a]){
         objs[z0].innerHTML=days[new Date(y,z0a,d).getDay()]+' '+objs[z0].innerHTML;
         break;
        }
       }
      }
     }
    }
    
    Format('msl_event_time');
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Thank you very much, works like a charm.


  •  

    Posting Permissions

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