JavaScript基础Date对象,字符串,Math

一、Date对象

<script type="text/javascript">
            // Date对象
            // 在js中使用Date对象来表示一个时间
            
            
            // 创建一个Date对象
            // 如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间
            var d = new Date();
            console.log(d);
            
            
            // 创建一个指定的时间对象
            // 需要在构造函数中传递一个表示时间的字符串作为参数
            var d2 = new Date("12/03/2016 11:10:30");
            console.log(d2);
            // getDate()获取当前日期对象是几号
            var date = d2.getDate();
            console.log("date = "+date);
            // getDay()获取当前日期对象是周几,会返回一个0到6的值
            // 0表示周日......
            var day = d2.getDay();
            console.log("day = "+day);
            // getMonth()获取当前日期对象的月份,会返回一个0到11的值
            // 0表示一月......
            var month = d2.getMonth();
            console.log("month = "+month);
            // getFullYear()获取当前日期对象的年份
            var year = d2.getFullYear();
            console.log(year);
            // getTime()获取当前日期对象的时间戳
            // 时间戳:指的是从格林威治标准时间的1970年1月1日0时0分0秒,到当前日期所花费的毫秒数
            // 1秒等于1000毫秒
            // 计算机底层在保存时间时使用的都是时间戳
            var time = d2.getTime();
            // 除以1000计算多少秒,除以第一个60计算分钟,除以第二个60计算小时,除以24计算天数,除以365计算多少年
            console.log(time/1000/60/60/24/365);
            
            
            // 获取当前的时间戳
            // 利用时间戳来测试代码的执行性能
            time = Date.now();
            console.log(time);
        </script>

二、字符串的方法

<script type="text/javascript">
            // 创建一个字符串
            var str = "hapuluosi";
            // 在底层字符串是以字符数组成的形式保存的
            // ["h","a","p","u","l","u","o","s","i"]
            
            // charAt()可以返回字符串中指定位置的字符
            // 根据索引获取指定的字符
            var result = str.charAt(0);
            console.log(result);
            // charCodeAt()可以获取指定位置字符的Unicode编码
            var result = str.charCodeAt(0);
            console.log(result);
            // fromCharCode()可以根据字符编码去获取字符
            var result = str.fromCharCode(20025);
            console.log(result);
            // concat()可以用来连接两个或多个字符串
            var result = str.concat("nb","666");
            console.log(result);
            // indexOf()该方法可以检索一个字符串中是否含有指定内容
            // 如果字符串中含有该内容则会返回其第一次出现的索引
            // 如果没有找到指定内容,则返回-1
            var result = str.indexof("h");
            var result = str.indexof("c");
            // 可以指定第二个参数,指定开始查找的位置
            var result = str.indexof("u",3);
            console.log(result);
            // lastIndexOf()该方法的作用和indexOf()一样(也可以指定开始查找的位置)
            // 不同的是indexOf()是从前往后找,lastIndexOf()是从后往前找
            var result = str.lastIndexOf("u");
            console.log(result);
            // slice()可以从字符串中截取指定内容
            // 不会影响到原字符串,而是将截取到的内容返回
            // 参数:
            // 第一个:开始位置的索引,包括开始位置
            // 第二个:结束位置的索引,不包括结束位置
            // 如果省略第二个参数,则会截取后面所有的元素
            // 也可以传递一个负数作为参数,将会从后面开始计算
            var result = str.slice(0,2);
            console.log(result);
            // substring()可以用来截取一个字符串,和slice()类似    
            // 参数:
            // 第一个:开始位置的索引,包括开始位置
            // 第二个:结束位置的索引,不包括结束位置
            // 不同的是这个方法不能接受负值作为参数
            // 如果传递一个负值,则默认为0传递
            // 而且会自动调整参数位置,如果第二个参数小于第一个,则参数自动交换位置
            var result = str.substring(0,2);
            console.log(result);
            // substr()用来截取字符串
            // 参数:
            // 第一个:截取开始位置的索引
            // 第二个:截取的长度
            var result = str.substr(0,2);
            console.log(result);
            // split()可以将一个字符串拆分成一个数组
            // 参数:需要一个字符串作为参数,会根据该字符串去拆分数组
            // 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的每个元素
            str = "abc,def,ghi"var result = str.split(",");
            console.log(result);
            // toUpperCase()将字符串转换为大写并返回
            // toLowerCase()将字符串转换为小写并返回
        </script>

三、Math

<script type="text/javascript">
            // Math和其他的对象不同,它不是一个构造函数
            // 它属于一个工具类不用创建对象,里面封装了数学运算相关的属性和方法
            // 比如:Math.PI表示圆周率
            console.log(Math.PI);
            
            
            // abs()可以用来计算一个数的绝对值
            console.log(Math.abs(-1));
            // Math.ceil()可以对一个数进行向上取整,小数位只要有值就自动进1
            console.log(Math.ceil(1.45));
            // Math.floor()可以对一个数进行向下取整,小数部分就自动舍去
            console.log(Math.floor(1.96));
            // Math.round()可以对一个数进行四舍五入取整
            console.log(Math.round(1.5));
            // Math.random()可以用来生成0到1之间的随机数
            for(var i=0;i<100;i++){
            // 生成0到x之间的随机数:Math.round(Math.random()*x)
            // 生成x到y之间的随机数:Math.round(Math.random()*(y-x)+x)
            }
            // max()可以获取到多个数中的最大值
            var max = Math.max(10,20,30);
            console.log(max);
            // min()可以获取到多个数中的最小值
            var min = Math.min(10,20,30);
            console.log(min);
            // Math.pow(x,y)返回x的y次幂
            console.log(Math.pow(2,3));
            // Math.sqrt()用于对一个数进行开方
            console.log(Math.sqrt(9));
        </script>
posted @ 2021-04-25 15:07  hapuluosi  阅读(88)  评论(0编辑  收藏  举报