Date日期对象以及时间戳

是一个构造函数

1、获取当前前时间

1、没有参数的情况

let date = new Date()

console.log(date)  //  当前系统时间
 
2、有参数的情况  (写法:数字形式和字符串形式  两种写法一样)


let date = new Date(2020,5,14)   //   , 号隔开

console.log(date) //  2020-06-13T16:00:00.000Z    返回的是 6 月 

let date = new Date('2020-5-14 8:8:8') 

console.log(date)   // 2020-05-14T00:08:08.000Z

2、格式化 日期 年 月 日 星期

let date = new Date()

console.log(date.getFullYear());  //返回当前年份

console.log(date.getMonth());   // 返回 月份小于1  记得加1 才是当前月份

console.log(date.getDate());   //返回的是几号

console.log(date.getDay());    //周一返回的是1,周六返回的是 6  周日返回的是0

一般想要星期几那么结合数组 

let arr = ['星期一',.....]

arr[date.getDay()]   //j就得到当前的星期啦

3、一些小案例

1、案例写一个  :    2019年5月1日星期三

let date = new Date()  

let Year = date.get FullYear()

 let month =date.getmontht() + 1 

let dates =date.getDate()

let arr= ['星期日','星期二','星期三、'星期四','星期五','星期六']


let day= date.getDay()

Console.1og(今天是:'+ Year +'4'+ month +' + dates+'日' + arr[day])

//今天是2019年5月1日星期三

2、格式化日期时分秒

let date = new Date()   // 没有参数都是当前时间

Console.log(date.getHours())

console.log(date. get Minutes())

console.log(date. getseconds())

3、案例 要求封装一个函数近回当前的时分秒格式是 08:08:08

function getTimer {

let time = new Date()

let h= time.getHours()

h=h<10?'0'+h:h1 //补0

let m = time.getminates()

m=m<10?0'+ m:m

let s = time.getSeconds () 

s= s < 10? 0'+ s: s

return   h +':'t + m + ': '+ s 

}

consele.109(getTimer())

4、获得 Date  的总毫秒数 

1、 写法一

let date = + new Date()

consile.log(date)

 2、写法二 

console.log (Date.now())    //记得加中间的点哦

5、倒计时效果

案例R计数果分析:①核心年法:输入的时间减去现在的时问就是利余的时间,即倒计时,但是不能拿着时分秒相
试,比如05分减去25分,结果是负数的②可以用时间群来做。用产输入时间论的亮秒数减去现在的总,的毫秒数,得到的就是乘余时问的亮承数③把剩余时间反总的亮秒数转换为天、时,分.秒(就是时间截转换为时分秒)转换公式如下:

d=Parselnt(总秒数/60/60/24)

h= Parselnt(总秒数/60/60%24

m =Parselnt(总秒数/60%60)

S=Parseint (总秒数 %60)

function coanDbun(time){

let nowDoTime = + new Date()

let inputTime = + new Date(time)

let times = (inputTime-ndu17oTime) 

let d = Parseint(time/60/60/24)  //天
let h= Parseint(time/60/60 %24)

h=h<10?'0'+h:h1 //补0

let m = Parseint(time/60%60)

m=m<10?0'+ m:m

let s = Parseint(time/60%) 

s= s < 10? 0'+ s: s

return d +'天’ + h + '时' + m + '分' + s + '秒'
}

conSole. 109 (coanDbun('2022-5-14 19:37:00'))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值