对象
JavaScript中的所有事物都是对象,如字符串、数值、函数、数组等。对象都有相关的属性和方法。
如数组的length属性、string对象的toUpperCase()方法。
Date对象
定义一个日期对象:
var mydate=new Date();此时mydate有初始值即当前时间(当前主机的系统时间)。
自定义日期对象的初始值
var date1=new Date(2014,10,13);
var date2=new Date('Oct 13,2014');
Date对象的方法:
getDate()/setDate() 返回/设置日期(日)
getFullYear()/setFullYear() 返回/设置年份 如2014
getYear()/setYear() 返回/设置年份 如114(相对于1900年)
getMonth()/setMonth() 返回/设置月份(0~11)
getDay()/setDay() 返回/设置星期 0表示星期天
getHours()/setHours() 返回/设置小时 24小时制
getMinutes()/setMinutes() 返回/设置分钟数
getSeconds()/setSeconds() 返回/设置秒数
getTime()/setTime() 返回/设置时间(以毫秒为单位)
示例:
时间推迟一小时
var mydate=new Date();
mydate.setTime(mydate.getTime()+60*60*1000);
String字符串对象
length属性
var mystr="Hello World!";
mystr.length;
toLowerCase方法
将字符串中的字符转换为小写。
charAt方法
charAt(index)返回指定位置index处的字符。
注:字符串中第一个字符的下标为0,最后一个为字符串长度减一。如果index的取值不在0~length-1之间,该方法将返回一个空字符串。
indexof()方法
返回指定的字符串值在字符串中首次出现的位置。如果指定的字符串值不在字符串中,则返回-1。
语法: stringObject.indexof(substring,startpos)