首页 > 编程语言 > 详细

JavaScript -- 常用的日期对象和数学对象

时间:2018-05-20 19:44:17      阅读:184      评论:0      收藏:0      [点我收藏+]

创建一个时间对象

var time=new Date();   //当前时间,()内可写入字符串 ,如‘2017-10-10‘,可转为响应的时间

常用方法

time.getFullYear();  //从 Date 对象以四位数字返回年份。

time.getMonth();    //从 Date 对象返回月份 (0 ~ 11)。

time.getDate();     //从 Date 对象返回一个月中的某一天 (1 ~ 31)。

time.getDay();     //从 Date 对象返回一周中的某一天 (0 ~ 6)。周日为0;

time.getHours();   //返回 Date 对象的小时 (0 ~ 23)。

time.getMinutes();     //返回 Date 对象的分钟 (0 ~ 59)。

time.getSeconds();     //返回 Date 对象的秒数 (0 ~ 59)。

time.getMilliseconds();    //返回 Date 对象的毫秒(0 ~ 999)。

time.getTime();        //返回 1970 年 1 月 1 日至今的毫秒数。

time.getTimezoneOffset();  //返回本地时间与格林威治标准时间 (GMT) 的分钟差。

数学对象

Math.abs(x);        //返回数的绝对值。

Math.ceil(x);       //进一取整

Math.floor(x);      //退一取整

Math.min(x,y);      //取最小值

Math.max(x,y);      //取最大值

Math.round(x);      //四舍五入

Math.random();      //0-1见的随机数,有0无1

Math.pow(x,y);      //x的y次幂

求一个min-max的随机数(且包括min和max):

function random(min,max){
    return Math.round(Math.random()*(max-min)+min);
}

JavaScript -- 常用的日期对象和数学对象

原文:https://www.cnblogs.com/adoctors/p/9064222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!