JavaScript【静态方法、实例方法/to类、实例方法/get类、实例方法/set类、Math与Date实操、 JS时间戳、日期互相转换】(九)

 

目录

Math对象_静态方法三

Date对象

Date对象_静态方法

Date对象_实例方法/to类

 Date对象_实例方法/get类

Date对象_实例方法/set类

Math与Date实操

 JS时间戳、日期互相转换


Math对象_静态方法三

Math.random() 

Math.random() 返回0到1之间的一个伪随机数,可能等于0,但是一定小于1

Math.random() // 0.28525367438365223

 任意范围的随机数生成函数如下

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
getRandomArbitrary(5, 10)

返回随机字符实例

function random_str(length) {
  var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
  ALPHABET += '0123456789-_';
  var str = '';
  for (var i=0; i < length; ++i) {
    var rand = Math.floor(Math.random() * ALPHABET.length);
    str += ALPHABET.substring(rand, rand + 1);
 }
  return str;
}
random_str(6) // "GEghrr"

三角函数方法

Math 对象还提供一系列三角函数方法

Math.sin():返回参数的正弦(参数为弧度值)
Math.cos():返回参数的余弦(参数为弧度值)
Math.tan():返回参数的正切(参数为弧度值)
Math.asin():返回参数的反正弦(返回值为弧度值)
Math.acos():返回参数的反余弦(返回值为弧度值)
Math.atan():返回参数的反正切(返回值为弧度值)

Date对象

Date 对象是 JavaScript 原生的时间库。它以1970年1月1日00:00:00 作为时间的零点,可以表示的时间范围是前后各1亿天(单位为毫秒)

普通函数的用法 

Date 对象可以作为普通函数直接调用,返回一个代表当前时间的字符串

Date()
'Tue Oct 26 2022 10:31:34 GMT+0800 (中国标准时间)'

 温馨提示

即使带有参数, Date 作为普通函数使用时,返回的还是当前时间

Date(2000, 1, 1)
// 'Tue Oct 26 2022 10:31:34 GMT+0800 (中国标准时间)'

 构造函数的用法

Date 还可以当作构造函数使用。对它使用 new 命令,会返回一个 Date 对象的实例。如果不加参数,实例代表的就是当前时间

var today = new Date();

作为构造函数时, Date 对象可以接受多种格式的参数,返回一个该参数对应的时间实例

// 参数为时间零点开始计算的毫秒数
new Date(1635215676627)
// Tue Oct 26 2021 10:34:36 GMT+0800 (中国标准时间)
// 参数为日期字符串
new Date('January 6, 2020');
// Mon Jan 06 2020 00:00:00 GMT+0800 (中国标准时间)
// 参数为多个整数,
// 代表年、月、日、小时、分钟、秒、毫秒
new Date(2020, 0, 1, 0, 0, 0, 0)
// Wed Jan 01 2020 00:00:00 GMT+0800 (中国标准时间)

各种类型的参数

new Date('2022-2-15')
new Date('2022/2/15')
new Date('02/15/2022')
new Date('2022-FEB-15')
new Date('FEB, 15, 2022')
new Date('FEB 15, 2022')
new Date('Feberuary, 15, 2022')
new Date('Feberuary 15, 2022')
new Date('15 Feb 2022')
new Date('15, Feberuary, 2022')

日期的运算

var d1 = new Date(2000, 2, 1);
var d2 = new Date(2000, 3, 1);
d2 - d1
// 2678400000   毫秒

Date对象_静态方法

时间戳 

时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间 1970年01月01日08时00分00秒)起至现在的总秒数。

格林威治和北京时间就是时区的不同

Unix是20世纪70年代初出现的一个操作系统,Unix认为1970年1月 1日0点是时间纪元。JavaScript也就遵循了这一约束

Date.now() 

Date.now 方法返回当前时间距离时间零点(1970年1月1日 00:00:00 UTC)的毫秒数,相当于 Unix 时间戳乘以1000

Date.now();   // 1635216733395

 Date.parse()

Date.parse 方法用来解析日期字符串,返回该时间距离时间零点 (1970年1月1日 00:00:00)的毫秒数

Date.parse('Aug 9, 2022')
Date.parse('January 26, 2022 13:51:50')
Date.parse('Mon, 25 Dec 2022 13:30:00 GMT')
Date.parse('Mon, 25 Dec 2022 13:30:00 +0430')
Date.parse('2022-10-10')
Date.parse('2022-10-10T14:48:00')

 如果解析失败,返回 NaN

 Date.parse('xxx') // NaN

Date对象_实例方法/to类

Date 的实例对象,有几十个自己的方法,分为三类 

to 类:从 Date 对象返回一个字符串,表示指定的时间。

get 类:获取 Date 对象的日期和时间。

set 类:设置 Date 对象的日期和时间。

Date.prototype.toUTCString() 

toUTCString 方法返回对应的 UTC 时间,也就是比北京时间晚8个小时

var d = new Date(2022, 0, 1);
d.toUTCString()
// "Fri, 31 Dec 2021 16:00:00 GMT"

Date.prototype.toDateString()

toDateString 方法返回日期字符串(不含小时、分和秒)

var d = new Date(2022, 0, 1);
d.toDateString() // "Sat Jan 01 2022"

 Date.prototype.toTimeString()

toTimeString 方法返回时间字符串(不含年月日)

var d = new Date(2022, 0, 1);
d.toTimeString() // 00:00:00 GMT+0800 (中国标准时间)

Date.prototype.toLocaleDateString()

toLocaleDateString 方法返回一个字符串,代表日期的当地写法(不含小时、分和秒)

var d = new Date(2022, 0, 1);
d.toLocaleDateString() // 2022/1/1

 Date.prototype.toLocaleTimeString()

toLocaleTimeString 方法返回一个字符串,代表时间的当地写法(不含年 月日)

var d = new Date(2022, 0, 1);
d.toLocaleTimeString() // 上午12:00:00

 Date对象_实例方法/get类

Date 对象提供了一系列 get* 方法,用来获取实例对象某个方面的值

实例方法get类

getTime():返回实例距离1970年1月1日00:00:00的毫秒数
getDate():返回实例对象对应每个月的几号(从1开始)
getDay():返回星期几,星期日为0,星期一为1,以此类推
getYear():返回距离1900的年数
getFullYear():返回四位的年份
getMonth():返回月份(0表示1月,11表示12月)
getHours():返回小时(0-23)
getMilliseconds():返回毫秒(0-999)
getMinutes():返回分钟(0-59)
getSeconds():返回秒(0-59)
var d = new Date('January 6, 2022');
d.getDate() // 6
d.getMonth() // 0
d.getYear() // 122
d.getFullYear() // 2022

编写函数获得本年度剩余天数

function leftDays() {
  var today = new Date();
  var endYear = new Date(today.getFullYear(), 11, 31, 23, 59, 59, 999);
  var msPerDay = 24 * 60 * 60 * 1000;
  return Math.round((endYear.getTime() - today.getTime()) / msPerDay);
}

Date对象_实例方法/set类

Date 对象提供了一系列 set* 方法,用来设置实例对象的各个方面

setDate(date) :设置实例对象对应的每个月的几号(1-31)
setYear(year) : 设置距离1900年的年
setFullYear(year [, month, date]) :设置四位年份
setHours(hour [, min, sec, ms]) :设置小时(0-23)
setMilliseconds() :设置毫秒(0-999)
setMinutes(min [, sec, ms]) :设置分钟(0-59)
setMonth(month [, date]) :设置月份(0-11)
setSeconds(sec [, ms]) :设置秒(0-59)
setTime(milliseconds) :设置毫秒时间戳
var d = new Date ('January 6, 2022');
d // Thu Jan 06 2022 00:00:00 GMT+0800 (中国标准时间)
d.setDate(9)
d // Sun Jan 09 2022 00:00:00 GMT+0800 (中国标准时间)
var d = new Date();
// 将日期向后推1000天
d.setDate(d.getDate() + 1000);
// 将时间设为6小时后
d.setHours(d.getHours() + 6);
// 将年份设为去年
d.setFullYear(d.getFullYear() - 1);

Math与Date实操

编写一个函数,获得一个十六进制的随机颜色的字符串(例 如:#20CD4F)方法

1、 编写一个函数

2、 十六进制:#000000 #FFFFFF: #0123456789abcdef

3 、十六进制的颜色之组成位数:6位

function genColor(){
    var str="0123456789abcdef";
    var color="#";
    for(var i=0;i<6;i++){
        var num=Math.floor(Math.random()*str.length);
        color=color+str[num];
   }
    return color;
}

 JS时间戳、日期互相转换

1、 时间戳转日期

2 、日期转时间戳

// 时间戳转日期
function timestampToTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
}
// 日期转时间戳
var date = new Date('2020-01-01 18:55:49:123');
var time1 = date.getTime();

 引入时间戳,生成可控长度的随机数

1、 随机数长度控制,定义一个长度变量(length),生成可控长度的随机数

2、 引入时间戳

3、 36进制:0-F (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ)

function genID(length){
   return Math.random().toString().substr(2,length) + Date.now().toString(36);
}

猜你喜欢

转载自blog.csdn.net/m0_58719994/article/details/132112302