zoukankan      html  css  js  c++  java
  • js之Math方法、字符串方法、date实例

    Math方法

    Math是一个对象数据类型

    在Math下有很多方法
    console.log(Math);
    console.log(typeof Math);

    1. Math.abs(): 取绝对值
    2. Math.floor(): 向下取整
    console.log(Math.floor(4.9999));//4
    console.log(Math.floor(-4.9999));//-5
    
    1. Math.ceil()向上取整
    2. Math.max() 获取一组数的最大值
    3. Math.min() 获取一组数的最小值
    4. Math.random() 产生一个[0,1)的随机小数
    5. Math.round() :四舍五入
    6. 产生一个m-n之间的随机整数
    Math.round(Math.random()*(n-m)+m)
    
    1. Math.pow(m,n);获取m的n次幂;
        console.log(Math.pow(4, 4));
    
    1. Math.sqrt : 开平方
        console.log(Math.sqrt(16));
    

    字符串方法

    1. toUpperCase : 把小写字母变成大写
      原字符串不变;
    2. toLowerCase : 把大写转小写
    3. charAt : 通过索引获取对应的字符
    console.log(str[3]);
    
    1. charCodeAt : 获取对应的字符的Unicode编码值;
    console.log(str.charCodeAt(3));
    // 97-122  a-z  65-90 A-Z
    
    1. substr(m,n) : 字符串截取;从索引m开始,截取n个;如果只有一个参数,截取到末尾
    console.log(str.substr(2));
    
    1. substring(m,n);从索引m开始,截取到索引n;但不包含n;
    2. slice(m,n);从索引m开始,截取到索引n;但不包含n;slice 支持负数传参;
    console.log(str.slice(2, -5))
    
    1. replace : 替换replace(old,new);用新字符串替换旧字符串
    2. indexOf : 检测字符串第一次出现的索引位置,如果不存在,返回-1;
    3. lastIndexOf:检测字符串最后一次出现的索引位置,如果不存在,返回-1;
    4. split: 按照特定的字符分隔成数组中的每一项;
    console.log(str.split(" "));
    
    1. concat : 字符串拼接;
    2. trim :去字符串的左右空格
      trimLeft() 去掉字符串左边的空格
      trimRight() 去掉字符串右边的空格
        console.log(str.trimLeft());
        console.log(str.length);
    

    Date的实例

    new + 函数: 创建这个函数的实例;实例是个对象数据类型;
    new 是个关键字;

    console.log(new Date());// 获取当前电脑的系统时间;
        var date = new Date();
        console.log(date);//Wed Dec 19 2018 17:09:34 GMT+0800
    
    1. getFullYear : 返回时间年;
      console.log(date.getFullYear());// 2018
    2. getMonth :返回时间月【0-11】
      console.log(date.getMonth());// 11
    3. getDate() : 返回当前日 【1-31】
      console.log(date.getDate());
    4. getDay: 返回星期几;【0-6】星期日是0;
      console.log(date.getDay());
      5. getHours : 返回小时
      console.log(date.getHours());// [0-23]
    5. getMinutes: 返回时间的分钟数
      console.log(date.getMinutes());//[0-59]
    6. getSeconds: 获取时间秒
      console.log(date.getSeconds());
    7. getMilliseconds: 获取毫秒数
      console.log(date.getMilliseconds());//[0-999]
      //console.log(typeof date);// "object"
        console.log(date.getTime());// 当前时间距离1970-1-1上午8:00 毫秒差
        console.log(Date.now());// 经常用于产生一个时间戳;产生一个唯一的数字;
    
  • 相关阅读:
    RESTful风格的API
    案例:toDoList
    jQuery中的Ajax
    php 使用kafka
    crontab不执行
    php两种实现守护进程的方式
    crontab不执行脚本,手动调测又没有任何问题
    centos7 安装跳板机(堡垒机)
    Ubuntu修改默认键盘布局的方法
    openresty nginx升级版
  • 原文地址:https://www.cnblogs.com/wangxingren/p/10160306.html
Copyright © 2011-2022 走看看