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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Formatting a date

    I have a string containing a date in the format YYYY-MM-DD and i want to format it so its something like 21st March 2012, how would i do this?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    echo date("dS F Y"strtotime($date)); // where $date contains the date in YYYY-MM-DD format 
    Edit: sorry, didn't notice the forum selected
    Last edited by abduraooft; 03-21-2012 at 01:07 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Here you are (suggest you ignore the unhelpful posts - webdev1958/bullant cannot resist making sneering comments, rather than offering any help):-

    Code:
    <script type = "text/javascript">
    
    var mths = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    
    var str = "2012-03-21";  // the date you specified YYYY-MM-DD
    var sp = str.split("-");
    var yr = sp[0];
    var mth = Number(sp[1])-1;  // months in javascript are 0-11
    var mthname = mths[mth];
    var day = Number(sp[2]);
    day = day + ["th","st","nd","rd"][!(day%10>3||Math.floor(day%100/10)==1)*day%10]; 
    var finaldate =  day + " " + mthname + " " + yr;
    alert (finaldate);
    
    </script>

    Obviously one having captured the component parts of the date you can manipulate their order to display them in any format you wish. You could also easily add the name of the weekday if you wished.


    "A good reputation can take years to aquire, a bad one takes a few seconds"
    Last edited by Philip M; 03-21-2012 at 12:30 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    tomharto (03-21-2012)

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Philip M View Post
    Here you are (suggest you ignore the unhelpful posts):-

    Code:
    <script type = "text/javascript">
    
    var mths = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    
    var str = "2012-03-21";  // the date you specified
    var sp = str.split("-");
    var yr = sp[0];
    var mth = Number(sp[1])-1;  // months in javascript are 0-11
    var mthname = mths[mth];
    var day = Number(sp[2]);
    day = day + ["th","st","nd","rd"][!(day%10>3||Math.floor(day%100/10)==1)*day%10]; 
    var finaldate =  day + " " + mthname + " " + yr;
    alert (finaldate);
    
    </script>

    Obviously one having captured the component parts of the date you can manuipulate their order to display them in any format you wish. You could also easily add the name of the weekday if you wished.


    "A good reputation can take years to aquire, a bad one takes a few seconds"

    Thank you, ill take a look at that soon as i get home

    EDIT: Tested it works like a charm thanks a lot
    Last edited by tomharto; 03-21-2012 at 01:41 PM.


  •  

    Posting Permissions

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