JS 对象:Date、Number、Math、console、表单

目录

JavaScript Date(日期) 对象

JavaScript Number(数字) 对象

JavaScript Math 数学计算

console 控制台对象

表单

document.表单名.submit() 表单提交

document.表单名.输入框名.value 表单取值


JavaScript Date(日期) 对象

1、Date 日期对象用于处理日期和时间,可以通过 new 关键词来定义 Date 对象。

2、JavaScript Date 对象 | 菜鸟教程

3、日期对象可以直接比较,如:new Date(1999, 7, 25) > new Date(1999, 4, 15)

// 初始化日期时间

let nowDate1 = new Date();// 获取当前系统时间,如:Sun Feb 05 2023 11:26:33 GMT+0800 (中国标准时间)
let nowDate2 = new Date("1993/8/25 8:5:1");
let nowDate3 = new Date("1993-08-25 08:05:01");
let nowDate4 = new Date(1999, 7, 25, 8, 5, 1);
let nowDate5 = new Date(746237101000);

getFullYear()获取年份。
getMonth()获取月份,[0,11],0 表示一月份,11表示十二月份。
getDate()获取月份中的天(1 ~ 31)。
getHours()获取日期中的小时数(0 ~ 23)。
getMinutes()获取日期中的分钟数(0 ~ 59)。
getSeconds()获取日期中的秒数(0 ~ 59)。
getMilliseconds()获取日期中的毫秒数(0 ~ 999)。
getTime()返回从 1970 年 1 月 1 日至今的毫秒数。
getDay()获取当前是星期几,[0,6],星期天是0。
toUTCString()将日期(根据 UTC)转换为字符串,如:Sun, 05 Feb 2023 03:38:27 GMT
toLocaleString将日期和时间转换为字符串,如 2023/2/5 11:51:01(分钟和秒会自动补0,月份和天不会补0)
toLocaleDateString将日期转换为字符串,如 2023/2/5
toLocaleTimeString将时间转换为字符串,如 11:59:02
toISOString以ISO格式的字符串值形式返回日期,如 2023-02-05T03:59:02.946Z
setTime(毫秒值)用1970年1月1日以来的毫秒值设置日期和时间
setFullYear(year[, month, date])使用本地时间设置Date对象的年份,也可以同时指定月份和天,0表示一月。
setMonth(month[, date])使用本地时间设置Date对象的月份,0表示一月,也可以同时指定天。
setDate使用本地时间设置Date对象一月中的某天。
setHours(hours[, min, sec, ms])使用本地时间设置Date对象中的小时值。也可以同时指定分钟、秒钟,毫秒。
setMinutes(min[, sec, ms])使用本地时间设置Date对象中的分钟值。也可以同时指定秒钟,毫秒。
setSeconds(sec[, ms])使用本地时间设置Date对象中的秒钟值。也可以同时指定毫秒。
// 获取日期与时间
let nowDate = new Date();
console.log("系统时间=" + nowDate);// Sun Feb 05 2023 11:26:33 GMT+0800 (中国标准时间)
console.log("系统时间的数字表示=" + nowDate.getTime());
// Sun, 05 Feb 2023 03:38:27 GMT
console.log("将当日的日期(根据 UTC)转换为字符串:" + nowDate.toUTCString())
// 2023/2/5 11:26:33、2023/2/5 11:51:01(分钟和秒会自动补0,月份和天不会补0)
console.log("将日期和时间转换为字符串=" + nowDate.toLocaleString());
console.log("将日期转换为字符串=" + nowDate.toLocaleDateString()); // 2023/2/5
console.log("将时间转换为字符串=" + nowDate.toLocaleTimeString()); // 11:26:33
console.log("以ISO格式的字符串值形式返回日期=" + nowDate.toISOString()); // 2023-02-05T03:59:02.946Z

console.log("年=" + nowDate.getFullYear());// 2023
console.log("月=" + nowDate.getMonth());// 1
console.log("日=" + nowDate.getDate());// 5
console.log("时=" + nowDate.getHours());// 11
console.log("分=" + nowDate.getMinutes());// 26
console.log("秒=" + nowDate.getSeconds());// 33(因为是数字,所以小于10时,前面不会自动补0)
console.log("毫秒=" + nowDate.getMilliseconds());// 839
console.log("星期=" + nowDate.getDay());// 0
// 初始化日期
let nowDate1 = new Date();
let nowDate2 = new Date("1993/8/25 8:5:1");// 1993/8/25 08:05:01
let nowDate3 = new Date("1993-08-25 08:05:01");// 1993/8/25 08:05:01
let nowDate4 = new Date(1999, 7, 25, 8, 5, 1);// 1993/8/25 08:05:01
let nowDate5 = new Date(746237101000);// 1993/8/25 08:05:01
let nowDate6 = new Date();// 1993/8/25 08:05:01
let nowDate7 = new Date();// 1993/8/25 08:05:01
nowDate1.setTime(746237101000);// 1993/8/25 08:05:01
nowDate6.setFullYear(1993);
nowDate6.setMonth(7);
nowDate6.setDate(25);
nowDate6.setHours(8);
nowDate6.setMinutes(5);
nowDate6.setSeconds(1);
nowDate7.setFullYear(1993, 7, 25);
nowDate7.setHours(8, 5, 1);

JavaScript Number(数字) 对象

JavaScript Number 对象 | 菜鸟教程

JavaScript Math 数学计算

1、Math 是 JavaScript 的内置对象,提供一系列数学常数和数学方法。该对象不是构造函数,不能生成实例,所有的属性和方法都必须在 Math 对象上调用,如 Math.floor(4.14)、Math.sqrt(16)...

2、JavaScript Math 对象 | 菜鸟教程

Math 对象常用方法
方法描述
abs(x)返回 x 的绝对值。如果 x 不是数字或者为 undefined ,则返回 NaN,如果 x 为 null 返回 0。
ceil(x)对数值进行上舍入。执行向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数
floor(x)对 x 进行下舍入。返回小于等于x的最大整数
max(x,y,z,...,n)

返回 x,y,z,...,n 中的最大值。

如果没有参数,则返回 -Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。

min(x,y,z,...,n)

返回 x,y,z,...,n中的最小值。

如果没有参数,则返回 -Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。

pow(x,y)返回 x 的 y 次幂。
random()返回介于 0(包含) ~ 1(不包含) 之间的一个随机小数,如 0.8110480700433211
round(x)把一个数字四舍五入为最接近的整数。
<script type="text/javascript">
    // 返回圆周率(约等于 3.141592653589793)
    let pi_v = Math.PI;
    // 返回算术常量 e,即自然对数的底数(约等于 2.718281828459045 )
    let e_v = Math.E;
    console.log("e=" + e_v, "pi=" + pi_v);
    // abs(x) 方法可返回一个数的绝对值。如果 x 不是数字返回 NaN,如果 x 为 null 返回 0。
    // 输出:10 20 30 NaN 0 NaN
    console.log(Math.abs(10), Math.abs(-20), Math.abs("30"), Math.abs("40abc"), Math.abs(null), Math.abs(undefined));
    // ceil() 方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。
    // 输出:1 1 2 -0 -0 -5 49
    console.log(Math.ceil(0.1), Math.ceil(0.8), Math.ceil(2), Math.ceil(-0.1), Math.ceil(-0.8), Math.ceil(-5), Math.ceil("48.9"));
    // floor() 方法返回小于等于x的最大整数。
    // 输出:0 0 2 -1 -1 -5 48
    console.log(Math.floor(0.1), Math.floor(0.8), Math.floor(2), Math.floor(-0.1), Math.floor(-0.8), Math.floor(-5), Math.floor("48.9"));
    // 返回 x,y,z,...,n 中的最大值。
    // 如果没有参数,则返回 -Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。
    console.log(Math.max(0, 150, 30, 20, 38));// 150
    // 返回 x,y,z,...,n中的最小值。
    // 如果没有参数,则返回 -Infinity。如果有某个参数为 NaN,或是不能转换成数字的非数字值,则返回 NaN。
    console.log(Math.min(0, 150, 30, 20, 38));// 0
    // 输出:3 5 6 -3 -4 -6 -10 20 100
    console.log(Math.round(2.60), Math.round(4.50), Math.round(6.49),
        Math.round(-2.60), Math.round(-4.50), Math.round(-6.49),
        Math.round(-10), Math.round(20), Math.round(100.0));
    // 输出[0,1]的随机整数
    console.log("[0,1]:" + Math.round(Math.random()));
    // 输出[0,100]的随机整数
    console.log("[0,100]:" + Math.round((Math.random() * 100)));
    // 输出[50,100]的随机整数
    console.log("[50,100]:" + Math.round((Math.random() * 50) + 50));
</script>

https://gitee.com/wangmx1993/web_app/blob/master/src/main/resources/static/MathTest.html

console 控制台对象

1、windown 的 console 对象的 log 方法用于在浏览器控制台输出信息,这个方法比alert(xx)方式的弹框调试更加方便! 

<script type="text/javascript">
    console.log("==========eval===普通运算=======");
    let age = 30;
    console.log(eval("1+3"));
    console.log(eval(age + 30));
    eval("a=3;b=5;document.write(a * b)");

    console.log("==========eval===解析JSON=======");
    let jsonStr = "{\"code\":\"9527\",\"name\":\"华安\"}";
    console.log(JSON.parse(jsonStr));
    // 同样可以解析为Json对象
    console.log(eval("(" + jsonStr + ")"));
</script>

表单

document.表单名.submit() 表单提交

1、标准语法:document.表单名.submit(); 用于延时form表单跳转。

2、直接用<input type="submit" name="button">,就是一个内置的用于提交表单的按钮,这样的按钮虽然很方便,但是它提交表单的时候不会经过js的表单验证。

3、<input name="button" name="tijiao" type="button" value="提交" οnclick="XXX()"> 这里的type="button",就是普通的按钮,它要实现提交表单的功能则必须用到js来实现,那么xxx()就是帮它实现的js函数了。在xxx()中document.表单名.submit(); 可以进行表单的提交。

document.表单名.输入框名.value 表单取值

<body>
<form action="https://home.firefoxchina.cn/" method="get" name="to_firefoxChina" target="_blank">
    隶属科室:<input type="text" name="c_department"><br>
    学历:<input type="text" name="c_qualification"><br>
    单位id:<input type="text" name="c_agency_id"><br>
    描述:<textarea style="resize: none" name="c_remark">
		</textarea>
    <button type="button" onclick="to_firefoxChina_fun()">提 交</button>
</form>
</body>
<script type="text/javascript">
    function to_firefoxChina_fun() {
        let department = document.to_firefoxChina.c_department.value;
        let qualification = document.to_firefoxChina.c_qualification.value;
        let agency_id = document.to_firefoxChina.c_agency_id.value;
        let remark = document.to_firefoxChina.c_remark.value;

        console.log("department:", department);
        console.log("qualification:", qualification);
        console.log("agency_id:", agency_id);
        console.log("remark:", remark);

        let isConfirm = confirm("是否确认前往火狐主页?");
        if (isConfirm) {
            document.to_firefoxChina.submit();
        }
    }
</script>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蚩尤后裔-汪茂雄

芝兰生于深林,不以无人而不芳。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值