Enjoy an ad free experience by logging in. Not a member yet?
Register .
09-10-2012, 09:14 PM
PM User |
#1
Regular Coder
Join Date: Feb 2005
Posts: 190
Thanks: 25
Thanked 0 Times in 0 Posts
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!
09-10-2012, 09:26 PM
PM User |
#2
Senior Coder
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
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];
Users who have thanked xelawho for this post:
09-10-2012, 09:26 PM
PM User |
#3
Regular Coder
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
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".
09-10-2012, 09:39 PM
PM User |
#4
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
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 ..
Users who have thanked Philip M for this post:
09-10-2012, 09:43 PM
PM User |
#5
Regular Coder
Join Date: Feb 2005
Posts: 190
Thanks: 25
Thanked 0 Times in 0 Posts
Brilliant thanks guys for the advise!
Amazingly quick too!
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 03:52 AM .
Advertisement
Log in to turn off these ads.