Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have the following code to display date

XML
<form>
<input type="text" name="date">
</form>
<!-- note the position -->
<script type="text/javascript">
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
document.forms[0].date.value = month + "/" + day + "/" + year;
</script>
</body>
</html>


but i need to show full month. The output of this is 7/20/2011 but i need it as 07/20/2011.

Any help would be appreciated.
Posted

1 solution

You could try something like this:

1. set the month variable to have leading zero
JavaScript
var month = "00" + (currentTime.getMonth() + 1);

2. then use the slice() function with a negative start index to get the last two characters
JavaScript
document.forms[0].date.value = month.slice(-2) + "/" + day + "/" + year;
 
Share this answer
 
v2
Comments
Member 8046452 20-Jul-11 17:16pm    
It is not working
Member 8046452 20-Jul-11 17:21pm    
I put one zero and works what if my month is 10i.e october?
Member 8046452 20-Jul-11 17:36pm    
It is working I change the things.

Thanks dude.
JOAT-MON 20-Jul-11 18:01pm    
Good deal! Glad you got it working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900