Math-Date

一.MATH

1:Math对象

Math 对象用于执行数学任务。

Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。

2:Math属性

PI:返回圆周率(约等于3.14159)。

console.log(Math.PI);

3:Math方法

Math.round(3.6);   // 四舍五入。

console.log(Math.round(3.2));

Math.random();     // 返回大于等于0到小于1之间的随机数。

console.log(Math.random());

Math.max(a, b);    // 返回较大的数值。

console.log(Math.max(4,5,3,2));

Math.min(a, b);    // 返回较小的数值。

console.log(Math.min(4,5,3,2));

Math.abs(num);     // 返回绝对值。

console.log(Math.abs(-4));

Math.ceil(3.6);    // 向上取整。

console.log(Math.ceil(3.1));

Math.floor(3.6);   // 向下取整。

console.log(Math.floor(-3.9));

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

console.log(Math.pow(4,3));

Math.sqrt(num);    // 开平方。

console.log(Math.sqrt(4));

4:随机数如何设定范围

案例:0 - 100(包含)之间的随机值

console.log(Math.round(Math.random() * 100));

案例:0 - 99(包含)之间的随机值

console.log(Math.floor(Math.random() * 100));

案例:1 - 100(包含)之间的随机值

console.log(Math.ceil(Math.random() * 100));

5:10进制转16进制或8进制

console.log((153).toString(16));//十进制转十六进制
console.log((153).toString(8));//十进制转八进制

 

二.DATE

1:日期对象

定义:JS DATE使用UTC(国际协调时间)1970,1,1,0,0,0,0所经过的毫秒数。

在JS中日期也是它的内置对象,所以我们要对日期进行获取和操作,必须实例化对象。

2:创建日期对象

var oDate=new Date();

将会包含本地时间的信息,包括年月日时分秒 星期。

var oDate = new Date();
console.log(oDate);

可以传入参数的格式:

1).“时:分:秒 月/日/年”、“月/日/年 时:分:秒”、“年/月/日”等字符串。

“时:分:秒 月/日/年”

var oDate = new Date('12:12:12 12/12/2018');
console.log(oDate);

“月/日/年 时:分:秒”

var oDate = new Date('12/12/2018 12:12:12');
console.log(oDate);

“年/月/日”

var oDate = new Date('12/12/2019');
console.log(oDate);

2). 年,月,日,时,分,秒。注意不能加“”。月是从0开始算的。

3). 时间戳。

3:日期对象获取信息的方法

注:月份和星期都是从0开始计算的。

// 时间戳:从1970年1月1日0时0分0秒0毫秒到某个时间点所经历的毫秒数

// 获取信息对象的方法

 var oDate = new Date();

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

console.log(oDate.getDate());

getMonth():从 Date 对象返回月份 (0 ~ 11)。

console.log(oDate.getMonth()); //0代表1月

getFullYear():从 Date 对象以四位数字返回年份。

console.log(oDate.getFullYear());

getDay():从 Date 对象返回一周中的某一天 (0 ~ 6)。 其中0代表星期日。

getYear():请使用 getFullYear() 方法代替。由 getYear() 返回的值不总是 4 位的数字!对于介于 1900 与 1999 之间的年份,getYear() 方法仅返回两位数字。对于 1900 之前或 1999 之后的年份,则返回 4 位数字!ECMAscript已经不再要求使用该函数

getHours():返回 Date 对象的小时 (0 ~ 23)。

 console.log(oDate.getHours());

getMinutes():返回 Date 对象的分钟 (0 ~ 59)。

console.log(oDate.getMinutes());

getSeconds():返回 Date 对象的秒数 (0 ~ 59)。

console.log(oDate.getSeconds());

getMilliseconds():返回 Date 对象的毫秒(0 ~ 999)。

console.log(oDate.getMilliseconds());

getTime():返回 1970 年 1 月 1 日至今的毫秒数。
console.log(oDate.getTime());
getDay():返回星期几。
console.log(oDate.getDay());

4:日期对象设置信息的方法

// 设置日期信息的方法

setDate():设置 Date 对象中月的某一天 (1 ~ 31)。

oDate.setDate(26);

setMonth():设置 Date 对象中月份 (0 ~ 11)。

oDate.setMonth(10);

setFullYear():设置 Date 对象中的年份(四位数字)。

oDate.setFullYear(2017);

setYear() 请使用 setFullYear() 方法代替。如果 year 参数是两位的数字,比如 setYear(91),则该方法会理解为 1991。如果要规定 1990 年之前或 1999 年之后的年份,请使用四位数字。 ECMAscript已经不再要求使用该函数

setHours():设置 Date 对象中的小时 (0 ~ 23)。

oDate.setHours(18);

setMinutes():设置 Date 对象中的分钟 (0 ~ 59)。

oDate.getMinutes(18);

setSeconds():设置 Date 对象中的秒钟 (0 ~ 59)。

oDate.setSeconds(001);

setMilliseconds():设置 Date 对象中的毫秒 (0 ~ 999)。

oDate.setMilliseconds(18);

setTime():以毫秒设置 Date 对象。

oDate.setTime(1516775284475);
console.log(oDate);

5:关于日期对象的常用操作

1:将日期格式化成字符串。

2:将指定格式字符串转化成日期对象。

/*
  
将日期格式化成字符串
   格式:YYYY-MM-DD HH:II:SS
*/

function formatDateToString() {
   
var oDate = new Date();
    var
       
year  = oDate.getFullYear(),
       
month = oDate.getMonth() + 1,
       
date  = oDate.getDate(),
       
hour  = oDate.getHours(),
       
minute= oDate.getMinutes(),
       
second= oDate.getSeconds();

    return
year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second;
}
/*
  
将指定格式的日期字符串转换成日期对象
   格式:YYYY-MM-DD HH:II:SS
*/

function formatStringToDate(str) {
   
var oDate = str.split(' ');

    var
left = oDate[0], right = oDate[1];

   
left = left.split('-');
   
right = right.split(':');

    return new
Date(left[0], left[1] - 1, left[2], right[0], right[1], right[2]);
}
console.
log(formatDateToString());
console.log(formatStringToDate('2017-10-9 12:36:23'));

3:日期字符串转为毫秒数。

/*
  
日期字符串转为毫秒数
   格式:YYYY-MM-DD HH:II:SS
*/

function formatStringToMilli(str) {
   
var oDate = formatStringToDate(str);
    return
oDate.getTime();
}
console.
log(formatStringToMilli('2017-10-9 12:36:23'));

4:计算两个日期的时间差值。

/*
  
计算两个日期的时间差值
   格式:YYYY-MM-DD HH:II:SS
*/

function diffDate(str1, str2) {
   
var
       
oDate1 = formatStringToDate(str1),
       
oDate2 = formatStringToDate(str2);
    return
oDate2.getTime() - oDate1.getTime();
}
console.
log(diffDate('2017-10-9 12:36:23', '2017-10-9 12:36:24'));

5:延时器和定时器

延时器:

语法:setTimeout(函数或者代码串,指定的时间(毫秒));

在指定的毫秒数后只执行一次函数或代码。

setTimeout(function(){
    console.
log('蹦');
},3000);

清除延迟器:clearTimeout();

setTimeout(function(){
   
clearTimeout(timer);
},2000);

定时器:

语法:setInterval(函数或者代码串,指定的时间(毫秒));

按照指定的周期(毫秒)不断的执行函数或者是代码串。

var num = 0;
setInterval(function(){
    console.
log(num++);
},1000);

清除定时器:clearInterval();

var num = 5;
var
timer = setInterval(function(){
    console.
log(--num);
    if
(num == 0){
       
clearInterval(timer);
   
}
}
,1000);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值