概述
目录
JavaScript Date(日期) 对象
JavaScript Number(数字) 对象
JavaScript Math 数学计算
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 (中国标准时间) | |
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 Math 数学计算
1、Math 是 JavaScript 的内置对象,提供一系列数学常数和数学方法。该对象不是构造函数,不能生成实例,所有的属性和方法都必须在 Math 对象上调用,如 Math.floor(4.14)、Math.sqrt(16)...
2、JavaScript 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
最后
以上就是着急自行车为你收集整理的JS 对象:Date、Number、MathJavaScript Date(日期) 对象JavaScript Number(数字) 对象JavaScript Math 数学计算的全部内容,希望文章能够帮你解决JS 对象:Date、Number、MathJavaScript Date(日期) 对象JavaScript Number(数字) 对象JavaScript Math 数学计算所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复