我有一个接受日期值的文本框,例如 01-01-2013 。我需要在javascript中获得一年。这是我用过的代码:
var oldRange, aOldRange;
var newRange, aNewRange;
aoldRange = new Array();
anewRange = new Array();
var oldYear, newYear;
oldRange = document.getElementById(
'<%= txtAcademicYearStartDate.ClientID%>').value;
aOldRange = oldRange.split("-");
oldYear = aoldRange[2];
但我得到的值是未定义的。有什么问题?
答案 0 :(得分:5)
您已定义aOldRange
,但引用了oldYear = aoldRange[2]
尝试
oldYear = aOldRange[2]
答案 1 :(得分:2)
此示例经过测试且工作正常,请参阅
<script language="javascript" >
var oldRange, aOldRange;
var newRange, aNewRange;
aoldRange = new Array();
anewRange = new Array();
var oldYear, newYear;
oldRange = '01-01-2013';
aOldRange = oldRange.split("-");
alert(aOldRange[2]);
</script>