• javascript中的Date对象和Math对象


    1.Date对象

    1.创建Date对象

    通过new方法创建对象

    var time1=new Date()
    

    方法1:不指定参数

    var time1=new Date();
    alert(time1.toLocaleString( ));
    

    方法2:参数为日期字符串

    var time2=new Date("2017/8/8 11:12");
    alert(time2.toLocaleString( ));
    var time3=new Date("2016/03/20 11:12");
    alert(time3.toLocaleString( ));
    

    方法3:参数为毫秒数

    var time3=new Date(5000);
    alert(time3.toLocaleString( ));
    alert(time3.toUTCString());
    

    方法4:参数为年月日小时分钟秒毫秒

    var time4=new Date(2004,2,20,11,12,0,300);
    alert(time4.toLocaleString( ));//毫秒并不直接显示
    

    2.Date对象的方法

    1.获取日期和时间

    getDate()                获取日
    getDay()                 获取星期
    getMonth()               获取月(0-11)
    getFullYear()            获取完整年份
    getYear()                获取年
    getHours()               获取小时
    getMinutes()             获取分钟
    getSeconds()             获取秒
    getMilliseconds()        获取毫秒
    getTime()                返回累计毫秒数(从1970/1/1午夜)
    

    例子:

    <script type="text/javascript" src="index.js">
        function getCurrentDate() {
            var date = new Date();              //创建Date对象,没有填入任何参数那么就是当前时间
            var year = date.getFullYear();      //获得当前年份
            var month = date.getMonth() + 1;    //获得当前月份js中月份是从0到11.
            var day = date.getDate();           //获得当前日
            var hour = date.getHours();         //获得当前小时
            var min = date.getMinutes();        //获得当前分钟
            var sec = date.getSeconds();        //获得当前秒
            var week = date.getDay();           //获得当前星期,没有getWeek
    
            // 2014年06月18日 15:40:30 星期三
            return year + "年" + changeNum(month) + "月" + day + "日 " + hour + ":" + min + ":" + sec + " " + parseWeek(week);
        }
    
        window.alert(getCurrentDate());
        //解决 自动补齐成两位数字的方法
        function changeNum(num) {
            if (num < 10) {
                return "0" + num;
            } else {
                return num;
            }
    
        }
        //将数字 0~6 转换成 星期日到星期六
        function parseWeek(week) {
            var arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
            //             0      1      2      3 .............
            return arr[week];
        }
    </script>
    

    用浏览器打开该文件,会弹出一个窗口,显示如下:

    2.设置日期和时间

    setDate(day_of_month)                               设置日
    setMonth (month)                                    设置月
    setFullYear (year)                                  设置年
    setHours (hour)                                     设置小时
    setMinutes (minute)                                 设置分钟
    setSeconds (second)                                 设置秒
    setMillliseconds (ms)                               设置毫秒(0-999)
    setTime (allms)                                     设置累计毫秒(从1970/1/1午夜)
    	
    var x=new Date();
    x.setFullYear (1997);                               //设置年1997
    x.setMonth(7);                                      //设置月7
    x.setDate(1);                                       //设置日1
    x.setHours(5);                                      //设置小时5
    x.setMinutes(12);                                   //设置分钟12
    x.setSeconds(54);                                   //设置秒54
    x.setMilliseconds(230);                             //设置毫秒230
    document.write(x.toLocaleString( )+"<br>");         //返回1997年8月1日5点12分54秒
    
    x.setTime(870409430000);                            //设置累计毫秒数
    document.write(x.toLocaleString( )+"<br>");         //返回1997年8月1日12点23分50秒
    

    3.日期和时间的转换

    getTimezoneOffset():8个时区×15度×4分/度=480;
    返回本地时间与GMT的时间差,以分钟为单位
    toUTCString()
    返回国际标准时间字符串
    toLocalString()
    返回本地格式时间字符串
    Date.parse(x)
    返回累计毫秒数(从1970/1/1午夜到本地时间)
    Date.UTC(x)
    返回累计毫秒数(从1970/1/1午夜到国际时间)
    

    2.Math对象

    1.Math对象中的属性方法

    abs(x)              返回数的绝对值。
    exp(x)              返回e的指数。
    ceil(x)				天花板函数,向上取整
    	如果参数x是整数,取整之后是参数本身
    	如果参数x是小数,对参数进行向上舍入
    floor(x)			对数进行下舍入。
    	如果参数x是整数,取整之后是参数本身
    	如果参数x是小数,对参数进行向下舍入
    log(x)              返回数的自然对数(底为e)。
    max(x,y)			返回 x 和 y 中的最高值。
    min(x,y)			返回 x 和 y 中的最低值。
    pow(x,y)			返回 x 的 y 次幂。
    random()			返回 0 ~ 1 之间的随机数。
    round(x)			对参数四舍五入
    sin(x)              返回数的正弦。
    sqrt(x)             返回数的平方根。
    tan(x)              返回角的正切。
    

    2.方法练习

    alert(Math.random()); 		// 获得随机数 0~1 不包括1.
    alert(Math.round(1.5));     // 四舍五入
    alert(Math.max(1,2));		// 2
    alert(Math.min(1,2));		// 1 
    alert(Math.pow(2,4));		// pow 计算参数2的4次方.
    

    练习:获取1-100的随机整数,包括1和100

    var num=Math.random();
    num=num*10;
    num=Math.round(num);
    alert(num)
  • 相关阅读:
    Codeforces Round #630 (Div. 2)A~E题解
    2020cug新生赛 An easy problem
    tensorflow.python.framework.errors.NotFoundError: <exception str() failed>错误解决
    将博客搬至CSDN
    2018年北京大学软件工程学科夏令营上机考试
    程序设计题目中的输入输出
    2018北大计算机学科夏令营机试题目
    Pyhton全栈的知识点(5)
    Python全栈的知识点(4)
    Python全栈的知识点(3)
  • 原文地址:https://www.cnblogs.com/renpingsheng/p/7309379.html
Copyright © 2020-2023  润新知