js中Math对象和Date对象

js中Math对象和Date对象Math 对象常用方法介绍 Math 是数学函数 但又属于对象数据类型 typeof Math gt object 它是一个对象 console dir Math 查看 Math 的所有函数方法 控制台打印的结果如下 1 Math abs 获取绝对值 Math abs 12

大家好,我是讯享网,很高兴认识大家。

Math对象常用方法介绍

控制台打印的结果如下:
在这里插入图片描述
讯享网

1,Math.abs() 获取绝对值

Math.abs(-12) = 12 

讯享网

2,Math.ceil() and Math.floor() 向上取整和向下取整

讯享网 console.log(Math.ceil(12.03));//13 console.log(Math.ceil(12.92));//13 console.log(Math.floor(12.3));//12 console.log(Math.floor(12.9));//12 

3,Math.round() 四舍五入
注意:正数时,包含5是向上取整,负数时包含5是向下取整。

1、Math.round(-16.3) = -16 2、Math.round(-16.5) = -16 3、Math.round(-16.51) = -17 4,Math.random() 取[0,1)的随机小数 

案例1:获取[0,10]的随机整数

讯享网console.log(parseInt(Math.random()*10));//未包含10 打印0-9的随机数 console.log(parseInt(Math.random()*10+1));//包含10 打印0-10的随机数 

案例2:获取[n,m]之间的随机整数

Math.round(Math.random()*(m-n)+n) 

在这里插入图片描述

5,Math.max() and Max.min() 获取一组数据中的最大值和最小值

讯享网console.log(Math.max(10,1,9,100,200,45,78)); //200 console.log(Math.min(10,1,9,100,200,45,78)); //1 

6,Math.PI 获取圆周率π 的值

console.log(Math.PI); 

在这里插入图片描述

7,Math.pow() and Math.sqrt()

讯享网1.Math.pow(10,2) = 100; 2.Math.sqrt(100) = 10; 

案例:自己定义一个对象,实现系统的max的方法

 function Mymax() { 
    //添加了一个方法 this.getMax = function () { 
    //假设这个数是最大值 var max = arguments[0]; for (var i = 0; i < arguments.length; i++) { 
    if (max < arguments[i]) { 
    max = arguments[i]; } } return max; }; } // 实例对象 var my = new Mymax(); console.log(my.getMax(9, 5, 6, 32)); //输出结果32 console.log(Math.max(9, 5, 6, 32)); //输出结果32 

Dath对象介绍

Date 对象 Date 对象用于处理日期和时间。

在这里插入图片描述
Dath属于一个函数

要创建一个日期对象,使用new操作符和Date构造函数即可:

讯享网var myDate = new Date(); 

注:Date 对象会自动把当前日期和时间保存为其初始值。

常用date函数

myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDate(); //获取当前日(1-31) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours(); //获取当前小时数(0-23) myDate.getMinutes(); //获取当前分钟数(0-59) myDate.getSeconds(); //获取当前秒数(0-59) myDate.getMilliseconds(); //获取当前毫秒数(0-999) myDate.toLocaleDateString(); //获取当前日期 var mytime=myDate.toLocaleTimeString(); //获取当前时间 myDate.toLocaleString( ); //获取日期与时间 

Date 对象属性
constructor 返回对创建此对象的 Date 函数的引用。
prototype 使您有能力向对象添加属性和方法。

简单常用示例

1.获取当前时间

讯享网function getNowTime() { 
    return new Date(); } 

2.时间与天数相加

function getTimeAddDays(time, days) { 
    return new Date(time.getTime() + days * 24 * 60 * 60 * 1000); } 

3.获取并格式化日期:年-月-日

讯享网function getFormatDate(time) { 
    return time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate(); } 

4.字符串转换为日期,字符串格式:2011-11-20

function convertToDate(strings) { 
    return new Date(Date.parse(strings.replace(/-/g, "/"))); } 

或者

讯享网function convertToDate(strings) { 
    return new Date(strings.replace(/-/g, "/")); } 
var str = "今天是星期" + "日一二三四五六".charat(new Date().getDay()); 

6.时间比较

讯享网function compareTime(time1, time2) { 
    return time1.getTime() - time2.getTime(); } 

7.计算两个日期之间相隔的天数

function getDays(time1, tiem2){ 
    var day = 24*60*60*1000; return (time1.getTime() - time2.getTime())/day; } 
小讯
上一篇 2025-02-19 07:07
下一篇 2025-03-11 09:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/91880.html