文本框中显示的日期 [英] Date to Display in textbox

查看:110
本文介绍了文本框中显示的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下代码来显示日期

Hi,

I have the following code to display date

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



但我需要显示整个月.此输出为2011年7月20日,但我需要为2011年7月20日.

任何帮助将不胜感激.



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.

推荐答案

您可以尝试以下方法:

1.将月份变量设置为前导零
You could try something like this:

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


2.然后使用带有负起始索引的slice()函数来获取最后两个字符


2. then use the slice() function with a negative start index to get the last two characters

document.forms[0].date.value = month.slice(-2) + "/" + day + "/" + year;


这篇关于文本框中显示的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆