Date对象和Math对象

这篇博客详细介绍了JavaScript中的Date对象和Math对象。在Date对象部分,讲解了如何创建Date对象,以及通过getXxx(), setXxx(), toXxx()等方法获取和设置日期时间,并进行格式转换。在Math对象部分,介绍了常用的数学运算方法,如ceil(), floor(), round(),以及生成随机数的技巧。" 91875916,8631863,Kubernetes容器运行时选择指南,"['Kubernetes', '容器技术', 'CRI接口', 'oci规范', '容器安全', '容器性能']

一、创建Date对象

var time=new Date()      //  获取的是当前时间

var time=new Date(2000)    // 1970年1月1日 8时0分2秒       20000是毫秒数
var time=new Date(98,10,20,0,0,0)       //  1998年10月10日 0时0分0秒

二、Date对象的方法

分3大组:
getXxx() : 用于获取时间和日期值
setXxx() : 用于设置时间和日期值
toXxx() : 将日期转换成指定格式

1. getXxx()

创建date对象,赋值给变量time

	var time=new Date()  // 获取当前时间
    document.write(time.toLocaleString())   //  2019/6/12 上午10:36:54

1.获取年份

	var year=time.getFullYear()  
    console.log(year)   // 2019年

2.获取月份

	var month=time.getMonth()   // 范围是0~11 所以获取的月份后面应该+1
    console.log(month+1)  // 6月

3.获取日期(某月份的多少号)

	var day=time.getDate()  // 范围是1~31号
    console.log(day)  // 12号

4.获取星期几

	var week=time.getDay()   // 范围是0~6
    console.log(week)  // 3  星期三

5.获取小时

	var hour=time.getHours()  // 范围是0~23
    console.log(hour)  // 10(早上10点)

6.获取分钟

	var minute=time.getMinutes()  // 范围是0~59
    console.log(minute)   // 44(第44分钟)

7.获取秒数

	var second=time.getSeconds()  // 获取秒数,范围是0~59
    var millisecond=time.getMilliseconds()  // 获取毫秒数,范围是0~999
    console.log(second)  //  28(第28分钟)
    console.log(millisecond)  //  856(第856毫秒)

8.获取1970年1月1日至今的毫秒数

	var a=time.getTime()
    console.log(a)  // 1560307994292(毫秒)
2. setXxx()

3. toXxx()

1.根据本地时间,将Date对象转换为字符串

	var time=new Date()  // 获取当前时间
    document.write(time)  // Wed Jun 12 2019 10:58:00 GMT+0800 (中国标准时间)
    document.write(time.toLocaleString())   //  2019/6/12 上午10:36:54

三、Math对象的方法

  1. ceil(x) : 对数进行上舍入=往大的取
	var a=3.1
    a=Math.ceil(a)
    console.log(a)  // 4
  1. floor(x) : 对数进行下舍入=往小的取
	var b=3.9
    b=Math.floor(b)
    console.log(b)  // 3
  1. round(x) : 四舍五入
	var c=3.6
    c=Math.round(c)
    console.log(c)  // 4  随机
  1. random() 返回0~1的随机数 [ 0 , 1 )
  2. parseInt(Math.random()*10) 随机整数[ 0 , 10 )
  3. parseInt(Math.random()*4+1) 随机整数[ 1 , 5 )
  4. parseInt(Math.random()*(max-min)+min) 随机整数 [ min , max )
  5. parseInt(Math.random()*(max-min+1)+min) 随机整数 [ min , max ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值