new Date 时间的常用方法,点赞收藏,很多你不知道

本文详细介绍了JavaScript中Date对象的多个方法,如getDate()用于获取日期中的日,getDay()获取星期几,getFullYear()获取年份,以及设置日期的各种方法如setDate(),setFullYear()等,还提到了时间戳和日期转换格式的方法。
摘要由CSDN通过智能技术生成
  1. getDate():获取日期中的日,返回值为1-31的整数。
const date = new Date();
console.log(date.getDate()); // 23
  1. getDay():获取日期中的星期几,返回值为0-6的整数,0表示星期日,1表示星期一,以此类推。
const date = new Date();
console.log(date.getDay()); // 2
  1. getFullYear():获取日期中的年份,返回值为4位数的整数。
const date = new Date();
console.log(date.getFullYear()); // 2021
  1. getHours():获取日期中的小时数,返回值为0-23的整数。
const date = new Date();
console.log(date.getHours()); // 10
  1. getMilliseconds():获取日期中的毫秒数,返回值为0-999的整数。
const date = new Date();
console.log(date.getMilliseconds()); // 123
  1. getMinutes():获取日期中的分钟数,返回值为0-59的整数。
const date = new Date();
console.log(date.getMinutes()); // 30
  1. getMonth():获取日期中的月份,返回值为0-11的整数,0表示一月,1表示二月,以此类推。
const date = new Date();
console.log(date.getMonth()); // 8
  1. getSeconds():获取日期中的秒数,返回值为0-59的整数。
const date = new Date();
console.log(date.getSeconds()); // 45
  1. getTime():获取日期的时间戳,返回值为从1970年1月1日00:00:00 UTC到当前日期的毫秒数。
const date = new Date();
console.log(date.getTime()); // 1632384645123
  1. getTimezoneOffset():获取当前时区与UTC时间的分钟差,返回值为负数表示当前时区比UTC时间早,正数表示当前时区比UTC时间晚。
const date = new Date();
console.log(date.getTimezoneOffset()); // -480
  1. setDate():设置日期中的日,参数为1-31的整数。
const date = new Date();
date.setDate(25);
console.log(date.getDate()); // 25
  1. setFullYear():设置日期中的年份,参数为4位数的整数。
const date = new Date();
date.setFullYear(2022);
console.log(date.getFullYear()); // 2022
  1. setHours():设置日期中的小时数,参数为0-23的整数。
const date = new Date();
date.setHours(12);
console.log(date.getHours()); // 12
  1. setMilliseconds():设置日期中的毫秒数,参数为0-999的整数。
const date = new Date();
date.setMilliseconds(500);
console.log(date.getMilliseconds()); // 500
  1. setMinutes():设置日期中的分钟数,参数为0-59的整数。
const date = new Date();
date.setMinutes(45);
console.log(date.getMinutes()); // 45
  1. setMonth():设置日期中的月份,参数为0-11的整数,0表示一月,1表示二月,以此类推。
const date = new Date();
date.setMonth(11);
console.log(date.getMonth()); // 11
  1. setSeconds():设置日期中的秒数,参数为0-59的整数。
const date = new Date();
date.setSeconds(30);
console.log(date.getSeconds()); // 30
  1. setTime():设置日期的时间戳,参数为从1970年1月1日00:00:00 UTC到当前日期的毫秒数。
const date = new Date();
date.setTime(1632384645123);
console.log(date.getTime()); // 1632384645123
  1. toDateString() - 将当前日期转换为字符串,格式为"星期 月 日 年"
    例:new Date().toDateString() // 返回类似"Wed Jul 20 2022"的字符串

  2. toISOString() - 将当前日期转换为ISO格式的字符串,格式为"yyyy-MM-ddTHH:mm:ss.sssZ"
    例:new Date().toISOString() // 返回类似"2022-07-20T06:30:45.123Z"的字符串

  3. toJSON() - 将当前日期转换为JSON格式的字符串,格式为"yyyy-MM-ddTHH:mm:ss.sssZ"
    例:new Date().toJSON() // 返回类似"2022-07-20T06:30:45.123Z"的字符串

  4. toLocaleDateString() - 将当前日期转换为本地日期格式的字符串
    例:new Date().toLocaleDateString() // 返回类似"2022/7/20"的字符串

  5. toLocaleString() - 将当前日期转换为本地日期时间格式的字符串
    例:new Date().toLocaleString() // 返回类似"2022/7/20 下午2:30:45"的字符串

  6. toLocaleTimeString() - 将当前时间转换为本地时间格式的字符串
    例:new Date().toLocaleTimeString() // 返回类似"下午2:30:45"的字符串

  7. toString() - 将当前日期转换为字符串,格式为"星期 月 日 年 HH:mm:ss 时区"
    例:new Date().toString() // 返回类似"Wed Jul 20 2022 14:30:45 GMT+0800 (中国标准时间)"的字符串

  8. toTimeString() - 将当前时间转换为字符串,格式为"HH:mm:ss 时区"
    例:new Date().toTimeString() // 返回类似"14:30:45 GMT+0800 (中国标准时间)"的字符串

  9. toUTCString() - 将当前日期转换为UTC时间格式的字符串
    例:new Date().toUTCString() // 返回类似"Wed, 20 Jul 2022 06:30:45 GMT"的字符串

  10. UTC() - 返回指定UTC时间的毫秒数
    例:Date.UTC(2022, 6, 20, 14, 30, 45) // 返回指定时间的毫秒数,比如 1655759445000

  11. valueOf() - 返回当前日期的毫秒数
    例:new Date().valueOf() // 返回当前日期的毫秒数,比如 1647460130123

  12. now() - 返回当前时间的毫秒数
    例:Date.now() // 返回当前时间的毫秒数,比如 1647460130123

还有.....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值