date对象
1.什么是date对象?
日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒)。
语法:var udate=new date();
注:初始值为当前时间(当前电脑系统时间)。
2.date对象常用方法:
3.date方法实例
复制代码 代码如下:
var newtime=new date();//获取当前时间
var millsecond=date.now();//当前日期转换成的毫秒数
var fullyear=newtime.getfullyear();//获取年份
var year=newtime.getyear();//获取年份
var month=newtime.getmonth();//获取月份 返回0-11 0表示一月 11表示十二月
var week=newtime.getday();//获取星期几 返回的是0-6的数字,0 表示星期天
var today=newtime.getdate();//获取当天日期
var hours=newtime.gethours();//获取小时数
var minutes=newtime.getminutes();//获取分钟数
var seconds=newtime.getseconds();//获取秒数
console.log(newtime);// wed feb 04 2015 10:54:17 gmt+0800 (中国标准时间)
console.log(millsecond);// 1423029309565
console.log(fullyear);// 2015
console.log(year);//115
console.log(month);//1 表示2月
console.log(week);//3 表示星期三
console.log(today);//4 4号
console.log(hours);//10小时
console.log(minutes);//54分钟
console.log(seconds);//17秒
math对象
1.什么是math对象?
math对象,提供对数据的数学计算。
注意:math 对象是一个固有的对象,无需创建它,直接把 math 作为对象使用就可以调用其所有属性和方法。这是它与date,string对象的区别。
2.math对象的属性和方法
math对象属性
math对象方法
3.math对象个别方法实例
1):ceil()方法向上取整,返回的是大于或等于x,并且与x最接近的整数。
复制代码 代码如下:
document.write(math.ceil(0.8) +
)//1
document.write(math.ceil(6.3) +
)//7
document.write(math.ceil(5) +
)//5
document.write(math.ceil(3.5) +
)//4
document.write(math.ceil(-5.1) +
)//-5
document.write(math.ceil(-5.9))//-5
2):floor()方法向下取整,返回的是小于或等于x,并且与x最接近的整数。
复制代码 代码如下:
document.write(math.floor(0.8) +
)//0
document.write(math.floor(6.3) +
)//6
document.write(math.floor(5) +
)//5
document.write(math.floor(3.5) +
)//3
document.write(math.floor(-5.1) +
)//-6
document.write(math.floor(-5.9))//-6
3):round() 方法可把一个数字四舍五入为最接近的整数
复制代码 代码如下:
document.write(math.round(0.8) +
)//1
document.write(math.round(6.3) +
)//6
document.write(math.round(5) +
)//5
document.write(math.round(3.5) +
)//4
document.write(math.round(-5.1) +
)//-5
document.write(math.round(-5.9)+
)//-6
4):random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。
复制代码 代码如下:
document.write(math.random());//返回0到1之间的数字 不包括1
document.write(math.random()*10);//返回0到10之间的数字 不包括10
5):min()方法:返回一组数值中的最小值
复制代码 代码如下:
document.write(math.min(2,3,4,6));//2
获取数组中最小值,使用apply()方法:
复制代码 代码如下:
var values=[3,2,1,8,9,7];
document.write(math.min.apply(math,values)+
);//1
math对象作为apply第一个参数,任意数组作为第二参数
6):max()方法:返回一组数值中的最大值
复制代码 代码如下:
document.write(math.max(2,3,4,6));//6
获取数组中最小值,使用apply()方法:
复制代码 代码如下:
var values=[3,2,1,8,9,7];
document.write(math.max.apply(math,values)+
);//9
以上就是关于javascript中date(日期对象),math对象的全部内容了,希望大家能够喜欢。