当前位置: 首页 > article >正文

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

  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

还有.....


http://www.kler.cn/a/14680.html

相关文章:

  • Ceph入门到精通- 选择硬件的一般原则
  • 摄影tips
  • 终端连接工具Tabby的下载、安装与配置
  • 网络:DPDK复习相关知识点
  • SpringMVC - REST风格介绍已经RESTful简化开发
  • 算法基础(三):链表知识点及题型讲解
  • MySQL高级篇——存储引擎和索引
  • Java线程详解
  • 【飞腾】遇到的问题与解决办法
  • SS524V100 RTL8152B(USB转网卡)驱动移植
  • 【Java基础】使用Java 8的Stream API来简化Map集合的操作
  • 【LeetCode: 5. 最长回文子串 | 暴力递归=>记忆化搜索=>动态规划 => 中心扩展法】
  • C/C++占位符,%x和%p的区别
  • 和chatgpt学习javascript,第一天,学习背景知识
  • 电源电压监测(SVD)
  • SpringBoot整合ELK做日志(超完整)
  • AR实战-基于Krpano的多场景融合及热点自定义
  • 基于Stackelberg博弈的光伏用户群优化定价模型(Matlab代码实现)
  • 什么是矩阵式项目管理?
  • Go | 一分钟掌握Go | 3 - 学习路线