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

Recommended Answers

All 2 Replies

Hi I try to do this within my function:

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

But it doesnt work ?

You can try this...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">
<HTML LANG="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Changin Date Format</TITLE>
<SCRIPT TYPE="text/javascript">
<!--
var now = new Date();
var month = ((now.getMonth())+1);
var dte = now.getDate();
var year = now.getFullYear();
month = ( month < 10 ? '0' : '' ) + month;
dte = ( dte < 10 ? '0' : '' ) + dte;

function changeDformat() {
var tmp = document.getElementById('date');

/* Assuming that the current date is set on the input field value. 
Then replace it later to any desired date format. (date format currently set to dd/mm/yyyy).
This code is tested in IE7 and Opera8+ */

if ( tmp ) { tmp.value = dte + '-' + month + '-' + year; 
mm = eval('tmp.value.match(/'+month+'/);');
dd = eval('tmp.value.match(/'+dte+'/);');
yyyy = eval('tmp.value.match(/'+year+'/);');

if ( mm != null && dd != null && year != null ) { // Now you can set your desired date format here //

tmp.value = mm + '/' + dd + '/' + yyyy; 
    } else { tmp.value = tmp.value; }
  }
}

if (window.addEventListener)
window.addEventListener('load',changeDformat,false);
else if (window.attachEvent)
window.attachEvent('onload',changeDformat)
else if (document.getElementById)
window.onload = changeDformat; 
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="#" onSubmit="return false;" ID="myForm">
<DIV>
<INPUT TYPE="text" ID="date" SIZE="20" VALUE="">
</DIV>
</FORM>
</BODY>
</HTML>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.