JS---时间对象

一、时间对象

1.1 作用

Date 对象用于处理日期和时间

1.2 创建方式

new Date()获取系统当前时间

代码示例:

var myDate=new Date(); //Tue Dec 24 2019 20:44:00 GMT+0800 (中国标准时间);
typeof myDate;====>"object"  // 获取到是一个对象,并不是字符串

1.3时间对象的相关属性和方法

常用方法总结:

方法

作用

getFullYear()

获取年

getMonth()

获取月 0到11 代表1月到12月

getDate()

获取日期

getDay()

星期几 (0---6)代表周日到到周六

getHours()

getMinutes()

getSeconds()

getMilliseconds()

毫秒

getTime()

获取当前日期到1970年1月1号 00:00:00 之间的毫秒差,时间戳

toLocaleString()

获取到的是年月日,时分秒"2019/12/25 上午10:15:50"

toLocaleDateString()

获取到是字符串的年月日,例如:"2019/12/25"

toLocaleTimeString()

获取到的是字符串的时分秒上午10:18:28

 示例代码:

        //   var date = new Date();
        //   console.log(date); //获取当前的时间

        //字符串  08--8月
        var date = new Date("2024-08-14 12:00:00");
        console.log(date);

        //直接写值  7---8月
        var date = new Date(2024, 7, 14, 12, 0, 0);
        console.log(date);

        var date = new Date();
        console.log(date); //获取当前的时间
        console.log(date.getFullYear()); //获取年
        console.log(date.getMonth()); //获取月 7  0-11  8月--7
        console.log(date.getDate()); //获取日
        console.log(date.getDay()); //获取星期几 3   1-6 0(星期日)
        console.log(date.getHours()); //获取小时
        console.log(date.getMinutes()); //获取分钟
        console.log(date.getSeconds()); //获取秒
        console.log(date.getMilliseconds()); //获取毫秒
        console.log(date.getTime()); //获取时间戳 获取当前日期到1970年1月1号 00:00:00 之间的毫秒差
        console.log(date.toLocaleString()); //当地的时间 2024/8/14 09:53:33
        console.log(date.toLocaleDateString()); //当地的时间 2024/8/14
        console.log(date.toLocaleTimeString()); //当地时间 09:54:14

二、结语

总结不易,留下你的足迹吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值