JavaScript创建和获取时间的方法

一、获取时间常用方法

1、创建时间对象

var time=new Date()       //创建当前的时间信息对象
var time1=new Date(2022,1,1,10,25,30)   //创建2022-1-1 10:25:30 的时间对象

2、获取年份信息

var year=time.getFullYear()      //time是时间对象

3、获取月份信息

var mouth=time.getMonth()        //0代表1月,11代表12月

4、获取日期信息

var  date=time.getDate()         //获取今天是几号

5、获取小时信息

var hours=time.getHours()      //获取当前是这天中的第几个小时

6、获取分钟信息

var minutes=time.getMinutes()       //当前这个小时数的第几分钟

7、获取秒钟信息

var seconds=time.getSeconds()   //获取当前分钟数的第几秒钟

8、获取一周中的第几天

var week=time.getDay()     //0代表周日,1-6代表星期一到六

9、获取时间戳信息

var  ms=time.getTime()    //从1970-1-1 0:0:0  到现在的毫秒数

二、设置时间信息:把获取时间的get变成set就可以了,但是没有设置星期

//直接设置时间
var time1=new Date(2030,5,10,20,30,10)   //创建2022-6-10 20:30:10 的时间对象


//单独设置时间
var time=new Date()              //创建时间对象
var year=time.setFullYear(2050)   //设置年份为2050年
var month=time.setMonth(2)        //设置月份为1月;0代表1月,11代表12月
var date=time.setDate(10)         //设置日期为10号
var hours=time.setHours(20)      //设置小时为晚上8点
var min=time.setMinutes(30)       //设置分钟为30分
var sec=time.setSeconds(30)        //设置秒钟为30秒
console.log(time)
//结果为:thu Mar 10 2050 20:30:30
//表示:2050年3月10日,20点30分30秒  星期四

三、例题练习

获取两个日期时间相差多少 天、小时、分钟、秒钟

function  getDiff(time1,time2){
    //获取两个时间的时间戳
    var ms1=time1.getTime()       //获取的是毫秒
    var ms2=time2.getTime()         //获取的是毫秒
     //获取两个的差值,向上取整,并取绝对值(除1000变成秒)
    var sub=Math.abs(Math.ceil((ms2-ms1)/1000))
    //获取天数(除60变成分钟数,再除变成小时数,再除24变成天数)
    var day=Math.floor(sub/(60*60*24))
    //获取小时数(先把天数模去剩下的再变成小时)
    var hours=Math.floor(sub%(60*60*24)/(60*60))
    //获取分钟数(把小时数模去,剩下的转换为小时)
    var minutes=Math.floor(sub%(60*60)/60)
    //获取秒钟数(把分钟数去掉还有多少秒)
    var seconds=sub%60
    
    //返回一个键值对对象
    return {day:day,hours:hours,minutes:minutes,seconds:seconds}
    
}
posted @   永恒之月TEL  阅读(350)  评论(1编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开
  1. 1 Welcome Home REOL
  2. 2 Everywhere We Go REOL
  3. 3 凄美地 REOL
  4. 4 最后一页 REOL
Welcome Home - REOL
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Benjamin Cooper

作曲 : Benjamin Cooper

Sleep don't visit, so I choke on sun

And the days blur into one

And the backs of my eyes hum with things I've never done

Sheets are swaying from an old clothesline

[01:08.0]Like a row of captured ghosts over old dead grass

Was never much but we made the most

Was never much but we made the most

Welcome home

Ships are launching from my chest

Ships are launching from my chest

Some have names but most do not

Some have names but most do not

you find If one, please let me know what piece I've lost

Heal the scars from off my back

I don't need them anymore

I don't need them anymore

You can throw them out or keep them in your mason jars

I've come home

I've come home

All my nightmares escaped my head

All my nightmares escaped my head

Bar the door, please don't let them in

Bar the door, please don't let them in

You were never supposed to leave

You were never supposed to leave

Now my head's splitting at the seams

Now my head's splitting at the seams

And I don't know if I can

And I don't know if I can

Here, beneath my lungs

Here, beneath my lungs

I feel your thumbs press into my skin again