2021-09-06

本文介绍了JavaScript中的包装器数据类型如何处理基本类型,如String的常用方法(长度、字符操作、索引查找等),以及Math对象的数学运算和日期时间处理,包括比较、舍入、随机数生成和Date对象的详细操作。
摘要由CSDN通过智能技术生成


1.包装器数据类型
     为了便于操作基本类型值,ECMAScript提供了3个特殊的引用类Boolean, Number, String。每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型对象,从而可以调用一些方法操作这些数据。
         基本数据类型
          var str='hello'
          str.replace()
   Boolean,Number,不建议直接使用这两种包装器类型
     String 类型:
         length        属性,获取字符串的字符数量
         charAt(i)        返回给定位置的字符
         charCodeAt(i)    返回给定位置的字符的字符编码
             例如:
                var s = "helloworld";
                s.charAt(1);       //e
                s.charCodeAt(1); //101
         indexOf()        从前往后查找指定字符所在位置
         lastIndexOf()        从后往前查找字符串所在位置,可以有第二个参数,代表从字符串中哪个位置开始查找。
         concat()    将一个或多个字符串拼接起来,返回拼接得到的新字符串,但是大多使用"+"拼接
         slice()    截取字符串(开始位置,结束位置)不包括结束位置的字符
         substr()    截取字符串(开始位置,截取字符个数)
         substring()    截取字符串(开始位置,结束位置)不包括结束位置的字符
                  var s = " helloworld";
                  s.slice(3,7);     //lowo
                  s.substr(3,7);     //loworld
                  s.substring(3,7);    //lowo
         trim()        删除前置以及后置中的所有空格,返回结果
         toLowerCase()    转换为小写
         toUpperCase()    转换为大写


2.Math对象
    (1)常用方法:
        1)比较方法
             Math.min()        //求一组数中的最小值
             Math.max()        //求一组数中的最大值
                 Math.min(1,2,19,8,6);    //1
       
        2)将小数值舍入为整数的几个方法
             Math.ceil()      向上舍入     => Math.ceil(12.41);    //13    
             Math.floor()     向下舍入     => Math.floor(12.41);    //12
             Math.round()     四舍五入     => Math.round(12.3);    //12         Math.round(12.5);    //13
       
        3)随机数
             Math.random()     //返回大于0小于1的一个随机数  [0,1)
    
    (2)其他方法:(了解即可,即用即查)
                abs(num)        返回num绝对值
                exp(num)        返回Math.E的num次幂
                log(num)        返回num的自然对数
                pow(num,power)    返回num的power次幂
                sqrt(num)        返回num的平方根
                scos(x)        返回x的反余弦值
                asin(x)        返回x的反正弦值
                atan(x)        返回x的反正切值
                atan2(y,x)        返回y/x的反正切值
                cos(x)        返回x的余弦值
                sin(x)        返回x的正弦值
                tan(x)        返回x的正切值

3.Data对象(时间)
    时间戳:指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。通俗的讲, 时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。

        第三方库 moment.js
             下载
             引入
             查文档使用
        第三方库 loadsh
            下载
             引入
             查文档使用
   
   (1)将一个字符串转换为Date对象
            var str = "2019-12-12";
            var date = new Date(str);     
              //字符串转换为Date对象
            document.write(date.getFullYear());    
              //然后就可以使用Date对象的方法输出年份了
   (2)Date的方法
          1)Date.prototype.getFullYear()
               返回年份  如2020。
                   var date = new Date();
                 document.write(date.getFullYear()); //返回2020,2020年
          
          2)Date.prototype.getYear()
               返回Date对象中的年份值减去1900    
          
          3)Date.prototype.getMonth()
                返回日期中的月份数,返回值0(1月)-11(12月)
                    var date = new Date();
                   document.write(date.getMonth()); //2020-4-15 此处返回4
                   (注意此处与通常理解有些偏差,1月份返回是0,12月返回是11)
          
          4)Date.prototype.getDate()
                返回是日期对象中的几号。
                  var date = new Date();             //2020-04-15
                  document.write(date.getDate());        //返回  15 是15号
         
          5)Date.prototype.getHours()    
                 返回日期中的小时,几点了,0-23
                   var date = new Date();
                   document.write(date.getHours()); //返回23,晚上11点
          
          6)Date.prototype.getMinutes()
                 返回日期中的分钟数 0-59
                    var date = new Date();
                    document.write(date.getMinutes());
                       //2020-4-15 23:22:10 返回22
          
          7)Date.prototype.getSeconds()
                返回一个日期的秒数
                  var date = new Date();
                document.write(date.getSeconds());     //返回34,2020-2-19 23:27:34 27分34秒
          
          8)Date.prototype.getDay()
                返回日期中的星期几 星期天0-星期六6
                    var date = new Date();
                    document.write(date.getDay());    //3 星期3
          
          9)Date.prototype.getMilliseconds()
                返回日期中的毫秒数
                   var date = new Date();
                 document.write(date.getMilliseconds());
                     //返回27 当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒
         
         10)Date.prototype.getTime()
                将一个日期对象以毫秒形式返回
                     var date = new Date();
                    document.write(date.getTime());
                       //返回1355930928466 返回值是1970-01-01 午夜到当前时间的毫秒数。
         
         11)Date.now()
                静态方法,获取当前时间的毫秒数
              document.write(Date.now());
                          //返回当前时间与1970-01-01的时间间隔,毫秒单位。
         
         12)Date.prototype.toString()
                将一个Date转换为一个字符串
                     var date = new Date();    //现在是2019-12-22
                    document.write(date.toString());//返回Sat Dec 22 2019 19:59:17 GMT+0800
          
          13)Date.prototype.toDateString()
                以字符串的形式返回一个Date的日期部分
                  var date = new Date();     
                   document.write(date.toDateString());
          
          14)Date.prototype.toTimeString()
                 以字符串的形式返回一个Date的时间部分
                   var date = new Date();     
                     document.write(date.toTimeString());
          
          15)Date.prototype.toLocaleString()
                将一个Date转化难为一个本地格式的字符串
                   var date = new Date();      
                    document.write(date.toLocaleString());
         
          16)Date.prototype.toLocaleDateString()
                 以本地格式的字符串返回一个Date的日期部分,返回一个本地人可读的日期格式,日期部分
                     var date = new Date();     
                     document.write(date.toLocaleDateString());
          
          17)Date.prototype.toLocaleTimeString()
                  将一个Date转化为本地的格式的时间部分
                     var date = new Date();     
                      document.write(date.toLocaleTimeString());
          
          18)Date.prototype.toISOString()
                   将一个Date对象转换为ISO-8601格式的字符串,返回的字符串格式为yyyy-mm-ddThh:mm:ssZ
                         var date = new Date();   
                        document.write(date.toISOString());
          
          19)Date.prototype.toJSON()
                  JSON序列化一个对象
                       var date = new Date();      
                         document.write(date.toJSON());

          20)Date.prototype.valueOf()
               如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示
                    var date = "";
                     document.write(date.valueOf());    //不是Date对象,不输出
                        var date1 = new Date();
                      document.write(date1.valueOf());   //输出1356180400916
          
           21)Date.prototype.getTimezoneOffset()
                  GMT格林威治标准时间与本地时间差,用分钟表示
                   北京时间=GMT时间+8小时
                        var date = new Date();
                          document.write(date.getTimezoneOffset());
                            //返回-480 实际上这个函数获取的是javascript运行于哪个时区。单位是分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
最新发布
06-03
这里是使用 Python 的 PyMySQL 完成上述任务的代码: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='test') # 创建游标 cur = conn.cursor() # 创建 users 表 cur.execute(''' CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(20), age INT ) ''') # 创建 orders 表 cur.execute(''' CREATE TABLE orders ( id INT PRIMARY KEY, order_date DATE, amount INT, user_id INT, FOREIGN KEY (user_id) REFERENCES users(id) ) ''') # 插入数据 users_data = [ (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28) ] orders_data = [ (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4) ] for user in users_data: cur.execute('INSERT INTO users VALUES (%s, %s, %s)', user) for order in orders_data: cur.execute('INSERT INTO orders VALUES (%s, %s, %s, %s)', order) # 提交事务 conn.commit() # 查询语句 # 1. 查询订单总金额 cur.execute('SELECT SUM(amount) FROM orders') print(cur.fetchone()[0]) # 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 cur.execute('SELECT ROUND(AVG(age), 2) FROM users') print(cur.fetchone()[0]) # 3. 查询订单总数最多的用户的姓名和订单总数。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_orders DESC LIMIT 1 ''') print(cur.fetchone()) # 4. 查询所有不重复的年龄。 cur.execute('SELECT DISTINCT age FROM users') print([row[0] for row in cur.fetchall()]) # 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 cur.execute('SELECT SUM(amount) FROM orders WHERE order_date BETWEEN "2021-09-01" AND "2021-09-04"') print(cur.fetchone()[0]) # 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders FROM users JOIN orders ON users.id = orders.user_id WHERE age <= 25 GROUP BY users.id ORDER BY total_orders DESC ''') print(cur.fetchall()) # 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC LIMIT 3 ''') print(cur.fetchall()) # 8. 查询订单总金额最大的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC LIMIT 1 ''') print(cur.fetchone()) # 9. 查询订单总金额最小的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount ASC LIMIT 1 ''') print(cur.fetchone()) # 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 cur.execute('SELECT * FROM users WHERE name LIKE "%李%" ORDER BY name ASC') print(cur.fetchall()) # 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 cur.execute('SELECT * FROM users WHERE age > 20 ORDER BY age DESC LIMIT 5') print(cur.fetchall()) # 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC ''') print(cur.fetchall()) # 关闭游标和连接 cur.close() conn.close() ``` 注意:在运行代码之前,需要先安装 PyMySQL 模块,可以使用以下命令进行安装: ``` pip install pymysql ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值