Results 1 to 4 of 4

Thread: ddmmyyyy to mmddyyyy in Javascript

  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ddmmyyyy to mmddyyyy in Javascript

    I have recently changed my page so my date that is held in a textbox is in the European Format, ie DD/MM/YYYY

    The problem is all of my other javascript functions expect it in the 'normal' format.
    Is there a way I can change the format I now have it in back, so:
    datevariable = document.getElementById('date').value;
    switches back from dd/MM/yyyy to MM/dd/yyyy

    Hi I try to do this within my function:

    Code:
    var tmp = document.getElementById('date').value;
    var tmp = ddmmyyyy.split('/');
    var date = tmp[1]+'/'+tmp[0]+'/'+tmp[2];

    But it doesnt work ?

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Can we see all the code? Also are you using PHP or JS to generate the time? (is it dynamic?)
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Here is a demo using JS

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    		<title>Untitled Document</title>
    		<style type="text/css">
    		</style>
    		<script type="text/javascript">
    			function changeDate(){
    				var currentDate= document.getElementById('date').value; //Extracting the date value in dd/mm/yyyy format from the mentioned text box
    				alert('The original value: ' + currentDate); //Printing the extracted date value before making the change
    				var newDate = currentDate.split('/'); //Splitting the extracted date value using the delimiter /, which is the seperator used in the date value			
    				currentDate = newDate[1] + "/" + newDate[0] + "/" + newDate[2];//Constructing a new date value (string) using the splitted values.
    				alert('Value after the change : ' + currentDate);//Showing the new date value.
    			}			
    		</script>
    	</head>
    	<body>
    		<form name="f1">
    			<input id="date" type="text" name="dte" value="07/03/2009" />
    			<input type="button" id="click" value="click"/>
    		</form>
    		<script type="text/javascript">
    			document.getElementById('click').onclick = changeDate; //Event handler assignment			
    		</script>
    	</body>
    </html>
    Hope this helps.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,920
    Thanks
    43
    Thanked 3,187 Times in 3,151 Posts
    Blog Entries
    12

    Default

    Assuming the rest of your code is correct, ddmmyyyy is undefined and you've declared tmp twice, which would overwrite the first instance of tmp, try:

    Code:
    var tmp = document.getElementById('date').value.split('/');
    var date = tmp[1]+'/'+tmp[0]+'/'+tmp[2];
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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