PDA

View Full Version : ddmmyyyy to mmddyyyy in Javascript



millsy007
03-06-2009, 09:18 PM
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:


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


But it doesnt work ?

Nile
03-06-2009, 09:33 PM
Can we see all the code? Also are you using PHP or JS to generate the time? (is it dynamic?)

codeexploiter
03-07-2009, 06:45 AM
Here is a demo using JS



<!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.

jscheuer1
03-07-2009, 07:09 AM
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:


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