javascript (日期)Date对象的一些使用和方法

一.从时间日期对象中获取具体的时间

// 从时间日期对象中获取具体的时间
var date = new Date()
console.log(date);
// 获取年份 - 对象.getFullYear()
var year = date.getFullYear()
console.log(year);
// 获取月份 - 对象.getMonth() - 在对象中,使用0~11来描述1~12月
var month = date.getMonth() + 1 // 因为获取到的月份会比实际月份小1,所以通常会+1
console.log(month);
// 获取日 - 对象.getDate()
var d = date.getDate() // 注意:千万不要使用date变量表示日
console.log(d);
// 获取星期 - 对象.getDay()
var day = date.getDay()
console.log(day);
// 获取时 - 对象.getHours()
var hour = date.getHours()
console.log(hour);
// 获取分 - 对象.getMinutes()
var minute = date.getMinutes()
console.log(minute);
// 获取秒 - 对象.getSeconds()
var second = date.getSeconds()
console.log(second);
// 获取毫秒 - 对象.getMilliseconds() - 1s === 1000ms
var mill = date.getMilliseconds()
console.log(mill);

二.设置日期时间

 设置具体时间

        setFullYear()

        setMonth() - 参数要比实际的月份少1

        setDate()

        setHours()

        setMinutes()

        setSeconds()

        setMilliseconds()

        setTime()

    注意:在设置时间中,没有设置星期几,因为星期几是根据年月日生成的,是不能被设置

例:

// 通过时间日期对象设置时间
var date = new Date() // 获取到当前时间对象
console.log(date);
// 将这个对象变成另外一个时间的对象 - 设置这个时间
// 设置年份:对象.setFullYear(目标年份)
date.setFullYear(2023)
console.log(date);
// 设置月份:对象.setMonth(目标月份) - 0~11来描述1~12月
date.setMonth(4) // 参数是0~11,参数:比实际月份-1
console.log(date);
// 设置日:对象.setDate(目标日)
date.setDate(25)
console.log(date);
// 设置时:对象.setHours(目标时)
date.setHours(15)
console.log(date);
// 设置分:对象.setMinutes(目标分)
date.setMinutes(50)
console.log(date);
// 设置秒:对象.setSeconds(目标秒)
date.setSeconds(59)
console.log(date);
// 设置毫秒:对象.setMilliseconds(目标毫秒数)
date.setMilliseconds(500)
console.log(date);
// 通过设置具体的时间,时间日期对象就发生了变化 - 通过设置以后的对象获取具体的时间
console.log( date.getMilliseconds() );

// 设置时间戳:对象.setTime(时间戳) - 将当前这个对象的时间切换成指定时间戳的对象
date.setTime(0)
console.log(date);

有关案例:

100天前是几月几号

思路:

思路分析:100以前,还是要根据当前时间逆推到100天

 需要当前时间日期对象 - 用当前时间-100天 - 用当前的时间戳-100天的毫秒数 - 100天以前的时间的时间戳

 根据100天以前的时间戳设置时间对象 - 得到100天以前的时间对象

 获取月份和日期

 var date = new Date()
    console.log(date)
    // 2.获取当前时间戳
    var time = date.getTime()
    // console.log(time)
    // 3.用当前的时间戳-100天的时间戳
    var time1 = 100 * 24 * 3600 * 1000 //得到100天的时间戳
    var time2 = time - time1 //得到的是1970年到一百天前的时间戳
    // console.log(time2)
    // 根据100天前的时间戳设置时间
    date.setTime(time2)
    // 获取月份和日期
    var mouth = date.getMonth() + 1;
    var day = date.getDate()
    console.log('100天前是' + mouth + '月' + day + '日')

三、创建指定时间的日期对象

  字符串参数:

    var date = new Date('2022-10-1 08:59:03')

    console.log(date);

    数字参数:注意 - 月份要比实际月份小1

     var date = new Date(2022,9,1,8,59,3)

     console.log(date);

    时间戳参数:

    var date = new Date()

    console.log(date);

    

    总结:

        new Date() - 没有参数获取到当前时间

        new Date('y年m月d日 h:i:s')

        new Date(y,m,d,h,m,s)

        new Date(time)

    

案例:现在距离国庆节还有多少天

思路:国庆的时间戳 - 当前的时间戳 = 毫秒差

根据毫秒差换算成天数

  // 1、获取国庆的时间日期对象
    var guoqing = new Date(2022, 10, 1)
    // console.log(guoqing)
    // 2、获取国庆的时间戳
    guoqing = guoqing.getTime()
    // console.log(guoqing)
    // 3.获取当前时间对象
    var now = new Date()
    // console.log(now)
    // 4、获取当前的时间戳
    now = now.getTime()
    // console.log(now)
    // 5.求两者的毫秒差
    var diff = guoqing - now;
    // console.log(diff)
    // 6.将毫秒换算为天数
    var day = parseInt(diff / 1000 / 60 / 60 / 24)
    console.log('距离国庆还有' + day + '天');

四、获取时间戳的方法

1..时间日期对象.getTime()

 var date= new Date()

 var time=date.getTime()

  console.log(time)

2.在new前面放+:

    // var date = +new Date()

    // console.log(date);

小补充:一个字符串的前面加+号可以将字符转为数字

3.利用构造函数Date:Date.parse('年-月-日 时:分:秒')

var guoqing=Date.parse(’2022-10-1‘)

console.log(guoqing)

五、格式化时间

正常情况下,我们在输出时间日期对象的时候,看起来特别不好看

// console.log( new Date() );

 所以这里介绍三个格式化时间的方法

1.格式化年月日

   var date = new Date()

    var str1 = date.toLocaleDateString()

    console.log(str1);

2。格式化时分秒

    var date = new Date()

    var str2 = date.toLocaleTimeString()

    console.log(str2);

3.前面两个的并写,格式化年月日时分秒

 var date = new Date()

    // 转成成更加方便看的时间显示 - 格式化输出时间

    // 格式化整个年月日时分秒

    var str = date.toLocaleString()

    console.log(str);

六、综合demo

1.定义函数,要求传入两个时间节点,能返回两个时间节点之间相差多少天多少小时多少分钟多少秒

function timeDiff(time1, time2) {
    var now = +new Date(time1)
    var guoqing = +new Date(time2)
    var diff = Math.abs(guoqing - now)
    console.log(diff);
    var day = parseInt(diff / 1000 / 60 / 60 / 24)
    var hour = parseInt(diff / 1000 / 60 / 60) % 24
    var minute = parseInt(diff / 1000 / 60) % 60
    var second = parseInt(diff / 1000) % 60
    // console.log('相差'+day+'天'+hour+'小时'+minute+'分钟'+second+'秒');

    return '相差'+day+'天'+hour+'小时'+minute+'分钟'+second+'秒'
}

var str = timeDiff('2022-03-18 14:10:00', '2022-10-01 00:00:00')
console.log(str);

2.编写函数,格式化当前时间为 "YYYY-MM-DD HH:ii:ss"

function formateDate(date) {
    // var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var d = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    // console.log(year, month, d, hour, minute, second);
    // console.log(year + "-" + month + '-' + d + ' ' + hour + ':' + minute + ':' + second);

    // 月/日/时/分/秒 如果他们是一位数字,给他补0
    // month = month < 10 ? '0' + month : month
    // d = d < 10 ? '0' + d : d
    // hour = hour < 10 ? '0' + hour : hour
    // minute = minute < 10 ? '0' + minute : minute
    // second = second < 10 ? '0' + second : second

    month = bu0(month)
    d = bu0(d)
    hour = bu0(hour)
    minute = bu0(minute)
    second = bu0(second)


    return year + "-" + month + '-' + d + ' ' + hour + ':' + minute + ':' + second
}

function bu0(num) {
    num = num < 10 ? '0' + num : num
    return num
}

var str = formateDate(new Date())
console.log(str);

猜你喜欢

转载自blog.csdn.net/weixin_45441470/article/details/123644633