JS基础 #第九天

目录

一、Date对象

二、Math

三、字符串的相关方法


一、Date对象

1.创建一个Date对象

    // 直接使用构造函数创建一个Date对象,则会封装为当前的时间
    var d = new Date();
    console.log(d); // Sun Aug 15 2021 14:07:19

2.创建一个指定的时间对象 格式:年/月/日 时:分:秒

    var d1 = new Date("2021/8/16 11:10:30");
    console.log(d1); // Mon Aug 16 2021 11:10:30

3.Date对象的常用方法:

方法释义

          getFullYear()

获取当前年份

         getMonth() + 1

获取的月份+1 = 获取的当前月份

           getDate()

获取当前日期的号数

           getDay()

获取当前是星期几

           getHours()

获取当前是几时

          getMinutes()

获取当前是几分

         getSeconds()

获取当前是几秒

             getTime()

 获取当前日期的时间戳

    d = new Date();
    var year = d.getFullYear();
    console.log(`当前为${year}年`); // 当前为2021年
    var month = d.getMonth() + 1;
    console.log(`当前为${month}月`); // 当前为8月 当前月份=获取的月份+1
    var date = d.getDate();
    console.log(`当前号数为${date}`); // 当前号数为15
    var day = d.getDay();
    console.log(`当前为周${day}`); // 当前为周0  0-6: 0表示周天 6表示周六
    var h = d.getHours();
    console.log(`当前为${h}时`); // 当前为14时
    var minutes = d.getMinutes();
    console.log(`当前为${minutes}分`); // 当前为42分
    var seconds = d.getSeconds(); 
    console.log(`当前为${seconds}秒`); // 当前为11秒
    // 打印当前时间
    console.log(`现在是${year}年${month}月${date}日${h}:${minutes}:${seconds}`); // 现在是2021年8月15日14:46:7

时间戳:指的是从1970年1月1日0时0分0秒到现在日期总的毫秒数(1秒 = 1000毫秒) 

    // 获取当前日期的时间戳
    var time = d.getTime();
    console.log(`当前日期的时间戳为${time}`); // 当前日期的时间戳为1629011247334
    
    // 获取当前时刻的时间戳
    time = Date.now();
    console.log(time); // 1629012131506

二、Math

Math常用方法

常用方法功能

Math.PI

表示圆周率

Math.abs()

用来计算一个数的绝对值

Math.ceil()

对一个数进行向上取整,小数位只要有值就自动进1

Math.floor()

对一个数进行向下取整,小数部分会被舍掉

Math.round()

对一个数进行四舍五入取整

Math.random()

用来生成一个(0,1)之间的随机数

Math.max()

获取多个数中的最大值

Math.min()

获取多个数中的最小值

Math.pow(x,y)

返回x的y次幂

Math.sqrt()

用于对一个数进行开方运算

    // 1.Math.PI 表示圆周率
    console.log(Math.PI); // 3.141592653589793 
    
    // 2.Math.abs()可以用来计算一个数的绝对值
    console.log(Math.abs(-1));  // 1

    // 3.Math.ceil() 可以对一个数进行向上取整,小数位只要有值就自动进1
    console.log(Math.ceil(1.4)); // 2
    console.log(Math.ceil(1.001)); // 2

    // 4.Math.floor() 可以对一个数进行向下取整,小数部分会被舍掉
    console.log(Math.floor(1.99)); // 1

    // 5.Math.round() 可以对一个数进行四舍五入取整
    console.log(Math.round(1.5)); // 2
    console.log(Math.round(1.4)); // 1

    // 6.Math.random() 可以用来生成一个(0-1)之间的随机数
    console.log(Math.random());
    /* 生成一个x-y之间的随机数  
      公式:Math.round(Math.random()*(y-x)+x) 
    */
    // 生成一个[10-20]之间的随机数
    console.log(Math.round(Math.random()*10+10));

    // 7.Math.max() 获取多个数中的最大值
    console.log(Math.max(10, 20, 30, 23)); // 30
    // 可以传变量
    var arr = [1, 2, 5, 32, 53];
    var max = Math.max.apply(null, arr);
    console.log(max); // 53
    
    // 8.Math.min() 获取多个数中的最小值
    console.log(Math.min(10, 20, 30, 23)); // 10
    
    // 9.Math.pow(x,y) 返回x的y次幂
    console.log(Math.pow(2, 3)); // 8
    
    // 10.Math.sqrt() 用于对一个数进行开方运算
    console.log(Math.sqrt(4)); // 2

注:

1.Math.random() 可以用来生成一个0-1之间的随机数,不包括0和1,若要生成一个x-y的随机数,并且要包括x、y,公式:Math.round(Math.random()*(y-x)+x)

2.Math.max() 用来获取多个数中的最大值,可以传变量,用apply来改变对象指向。Math.min()同理。

三、字符串的相关方法

1.在底层字符串是以字符数组的形式保存的,可以用str.length属性来获取字符串的长度,用str[下标]来获取字符串里面的元素,下标从0开始。

    // 用str.length属性来获取字符串的长度
    var str = 'hello';
    console.log(str.length); // 5

    // 用str[下标]来获取字符串里面的元素,下标从0开始
    for (var i = 0; i < str.length; i++) {
      console.log(str[i]); // h e l l o
    }

2.常用方法:以下方法都不会影响到原数组

方法功能
charAt()

根据索引获取指定的字符,和str[i]功能类似

charCodeAt()

根据索引获取指定字符的字符编码(Unicode编码)

string.fromCharCode()

根据字符编码去获取字符

concat()

连接两个或多个字符串 作用和+一样

indexof()

根据字符返回其第一次出现时候的索引,从前往后找

lastIndexOf()

根据字符返回其第一次出现时候的索引,从后往前找

slice()

从字符串中截取指定的内容

substring()

截取一个字符串,和slice()类似

substr()

用来截取字符串 (了解,非标准化)

split()

将一个字符串拆分为一个数组

 toUpperCase()

 将字符串里面的元素转换为大写

toLowerCase() 

将字符串里面的元素转换为小写

    // 1.charAt() 根据索引获取指定的字符 不会改变原数组
    // 和str[i]功能类似
    str = 'hello world';
    var re = str.charAt(6);
    console.log(re); // w

    // 2.charCodeAt() 根据索引获取指定字符的字符编码(Unicode编码)
    re = str.charCodeAt(6);
    console.log(re); // 119

    /* 3. string.fromCharCode()
        可以根据字符编码去获取字符
    */
    re = String.fromCharCode(72);
    console.log(re); // H

    // 4.concat() 连接两个或多个字符串 作用和+一样
    re = str.concat('你好', '世界');
    console.log(re); // hello world你好世界

    // 5.indexof() 
    // 5.1根据字符返回字符的下标 会返回其第一次出现的索引
    re = str.indexOf("o");
    console.log(re); // 4
    // 5.2没有该字符则返回-1 可以用来检索一个字符串中是否含有指定字符 
    re = str.indexOf("c");
    console.log(re); // -1
    // 5.3可以指定第二个参数,表示开始查找的位置
    re = str.indexOf("o", 5);
    console.log(re); // 7

    /* 6.lastIndexOf() 该方法的用法和indexof()一样,区别是:
    indexOf是从前往后找
    lastIndexOf是从后往前找 */
    re = str.lastIndexOf("l");
    console.log(re); // 9
    // 6.1没有该字符则返回-1
    re = str.lastIndexOf("p");
    console.log(re); // -1
    // 6.2可以指定第二个参数,表示开始查找的位置
    re = str.lastIndexOf("l", 8);
    console.log(re); // 3

    /*
      7.slice()
        可以从字符串中截取指定的内容
        不会影响原字符串,而是将截取的内容返回
        参数:
          第一个,开始位置的索引(包括开始位置)
          第二个,结束位置的索引(不包括结束位置)
    */
    str = 'abcdefghjk';
    re = str.slice(1, 3);
    console.log(re); // bc
    //  7.1如果省略第二个参数,则会截取到后边所有的
    re = str.slice(1);
    console.log(re); // bcdefghjk
    // 7.2也可以传递一个负数作为参数,负数的话将会从后边计算,并且包括结束位置
    re = str.slice(1, -2);
    console.log(re); // bcdefgh

    /*
      8. substring()
        可以用来截取一个字符串,和slice()类似,区别是:
          substring()不能传负值,如果传递了一个负值,则默认使用0
          如果第二个参数小于第一个,则自动交换位置
        参数:
          第一个:开始截取位置的索引(包括开始位置)
          第二个:结束位置的索引(不包括结束位置)
    */
    re = str.substring(1, 2);  
    console.log(re); // b
    // 8.1传递负值,默认使用0
    re = str.substring(1, -2);
    console.log(re); // a
    // 8.2第二个参数小于第一个,则自动交换位置
    re = str.substring(2, 1);
    console.log(re); // b

    /*
      9.substr() 用来截取字符串 (了解,非标准化)
      参数:
        第一个:截取开始位置的索引
        第二个:截取的长度
    */
    str = 'abcdefg';
    re = str.substr(0, 2);
    console.log(re); // ab

    /*
      10.split();
        可以将一个字符串拆分为一个数组
        参数:
          需要指定根据字符串里面的哪一个字符去拆分
     */
     str = 'abc, bc, sd, sb';
     re = str.split(', ');
     console.log(re); //  ["abc", "bc", "sd", "sb"]
    // 10.1如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素
    str = 'abcdefghi';
    re = str.split('');
    console.log(re); //  ["a", "b", "c", "d", "e", "f", "g", "h", "i"]

    // 11. toUpperCase() 将字符串里面的元素转换为大写
    str = 'abcde';
    re = str.toUpperCase();
    console.log(re); // ABCDE

    
    // 12. toLowerCase() 将字符串里面的元素转换为小写
     str = 'ABCDEFG';
     re = str.toLowerCase();
     console.log(re); // abcdefg

注:

1.slice() 可以从字符串中截取指定的内容

1.1参数:

      第一个,开始位置的索引(包括开始位置)

      第二个,结束位置的索引(不包括结束位置),如果省略第二个参数,则会截取到后边所有的元素

1.2也可以传递一个负数作为参数,负数的话将会从后边计算,并且包括结束位置

2.substring() 可以用来截取一个字符串,和slice()类似,区别是:substring()不能传负值,如果传递了一个负值,则默认使用0。

2.1如果第二个参数小于第一个,则自动交换位置

2.2参数:

        第一个:开始截取位置的索引(包括开始位置)

        第二个:结束位置的索引(不包括结束位置)

2.3如果没有设置参数,直接返回整个字符串

3.split() 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值