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 09-10-2012, 09:14 PM   PM User | #1
vorl
Regular Coder

 
Join Date: Feb 2005
Posts: 190
Thanks: 25
Thanked 0 Times in 0 Posts
vorl is an unknown quantity at this point
Onload change date format in text box

Hi there

This problem sounds easy in my head but I just cant seem to do or find anything which works!

I have a textbox which has the date in it. The format is DD/MM/YY (ie. 10/09/12).

Onload, I would like to change the date in the textbox to format "10 September 2012".

The only reason for this change in date format is purely aesthetics

Any help appreciated. Thanks all!
vorl is offline   Reply With Quote
Old 09-10-2012, 09:26 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Code:
var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var bits=document.getElementById("yourtextboxid").value.split("/");
document.getElementById("yourtextboxid").value=months[bits[1]-1]+" "+bits[0]+" "+"20"+bits[2];
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
vorl (09-10-2012)
Old 09-10-2012, 09:26 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
Set a window.onload = functionName; or use the jQuery .ready() to contain the code.

Get the value of the textbox.

Split the value using / as the delimiter.

Leave the date as is; set up a switch/case to change the second to the full name (ie case 1: "January", case 2: "February", etc.); add 2000 to the year.

Repopulate the textbox with the new value.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 09-10-2012, 09:39 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
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
Code:
<html>
<head>
</head>
<body onload = "changedate()">
<input type = "text" id = "mydate" value = "10/09/12">

<script type = "text/javascript">
function changedate() {
var m = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var ds = document.getElementById("mydate").value.split("/");
var mth = Number(ds[1])-1;  // array index starts at 0
var newdate = ds[0] + " " + m[mth] + " " + (Number (ds[2])+2000);
}
</script>

</body>
</html>

Portuguese Water Dog Pups - adorable mostly black President Obama has one. Deposits now being taken. £680. - Ad in Exchange and Mart
__________________

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; 09-10-2012 at 09:43 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
vorl (09-10-2012)
Old 09-10-2012, 09:43 PM   PM User | #5
vorl
Regular Coder

 
Join Date: Feb 2005
Posts: 190
Thanks: 25
Thanked 0 Times in 0 Posts
vorl is an unknown quantity at this point
Brilliant thanks guys for the advise!

Amazingly quick too!
vorl 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 03:52 AM.


Advertisement
Log in to turn off these ads.