Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2012, 11:44 AM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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?
tomharto is offline   Reply With Quote
Old 03-21-2012, 12:05 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 03-21-2012 at 01:07 PM..
abduraooft is offline   Reply With Quote
Old 03-21-2012, 12:18 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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"
__________________

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.

Last edited by Philip M; 03-21-2012 at 12:30 PM.. Reason: Typo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
tomharto (03-21-2012)
Old 03-21-2012, 12:22 PM   PM User | #4
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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..
tomharto is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:14 PM.


Advertisement
Log in to turn off these ads.