码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Day.js 基本使用


    Day.js 基本使用

    文章目录

    • Day.js 基本使用
    • 一、概述
      • 1、中文网
      • 2、简介
    • 二、基本使用
      • 1、安装
      • 2、基本使用
      • 3、获取当前时间
      • 4、根据时间字符串创建时间对象
      • 5、根据时间戳创建时间对象
      • 6、根据 Date 对象创建时间对象
      • 7、获取年份
      • 8、获取月份
      • 9、获取日期
      • 10、传入对象
      • 11、传入数组
      • 12、UTC 时间
      • 13、获取当前时间戳
      • 14、Dayjs 复制
      • 15、dayjs 对象设置年份
    • 三、操作
      • 1、加上
      • 2、减去
      • 3、时间的开始
      • 4、时间的结束
    • 四、总结

    一、概述

    1、中文网

    https://dayjs.fenxianglu.cn/

    2、简介

    Day.js 是一个极简的 JavaScript 库,可以为现代浏览器解析、验证、操作和显示日期和时间。

    二、基本使用

    1、安装

    pnpm add dayjs
    
    • 1

    2、基本使用

    https://dayjs.fenxianglu.cn/category/#typescript

    import dayjs from "dayjs";
    console.log("=====>", dayjs().format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2023-09-22 14:56:11
    
    • 1
    • 2
    • 3

    3、获取当前时间

    import dayjs from "dayjs";
    console.log("=====>", dayjs().format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2023-09-22 14:56:11
    
    • 1
    • 2
    • 3

    4、根据时间字符串创建时间对象

    import dayjs from "dayjs";
    console.log("=====>", dayjs("2018-08-08").format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2018-08-08 00:00:00
    
    • 1
    • 2
    • 3

    5、根据时间戳创建时间对象

    import dayjs from "dayjs";
    console.log("=====>", dayjs(1318781876406).format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2011-10-17 11:51:16
    
    • 1
    • 2
    • 3

    6、根据 Date 对象创建时间对象

    import dayjs from "dayjs";
    console.log("=====>", dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2021-09-22 14:56:11
    
    • 1
    • 2
    • 3

    7、获取年份

    import dayjs from "dayjs";
    console.log("=====>", dayjs().year());
    // =====> 2021
    
    • 1
    • 2
    • 3

    8、获取月份

    import dayjs from "dayjs";
    console.log("=====>", dayjs().month());
    // =====> 8
    
    • 1
    • 2
    • 3

    9、获取日期

    import dayjs from "dayjs";
    console.log("=====>", dayjs().date());
    // =====> 22
    
    • 1
    • 2
    • 3

    10、传入对象

    import dayjs from "dayjs";
    console.log(
      "=====>",
      dayjs({ year: 2018, month: 8, day: 8 }).format("YYYY-MM-DD HH:mm:ss")
    );
    // =====> 2018-08-08 00:00:00
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    11、传入数组

    import dayjs from "dayjs";
    console.log("=====>", dayjs([2018, 8, 8]).format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2018-08-08 00:00:00
    
    • 1
    • 2
    • 3

    12、UTC 时间

    import dayjs from "dayjs";
    console.log("=====>", dayjs.utc().format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2021-09-22 06:56:11
    
    • 1
    • 2
    • 3

    13、获取当前时间戳

    import dayjs from "dayjs";
    console.log("=====>", dayjs().valueOf());
    // =====> 1632290171000
    
    • 1
    • 2
    • 3

    14、Dayjs 复制

    import dayjs from "dayjs";
    const dayjs1 = dayjs();
    const dayjs2 = dayjs1.clone();
    console.log("=====>", dayjs1 === dayjs2);
    // =====> false 是两个独立的 Day.js 对象
    
    • 1
    • 2
    • 3
    • 4
    • 5

    15、dayjs 对象设置年份

    import dayjs from "dayjs";
    console.log("=====>", dayjs().set("year", 2018).format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2018-09-22 14:56:11
    
    • 1
    • 2
    • 3

    三、操作

    1、加上

    import dayjs from "dayjs";
    console.log("=====>", dayjs().add(1, "year").format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2022-09-22 14:56:11
    
    • 1
    • 2
    • 3

    2、减去

    import dayjs from "dayjs";
    console.log(
      "=====>",
      dayjs().subtract(1, "year").format("YYYY-MM-DD HH:mm:ss")
    );
    // =====> 2020-09-22 14:56:11
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、时间的开始

    import dayjs from "dayjs";
    console.log("=====>", dayjs().startOf("year").format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2021-01-01 00:00:00
    
    • 1
    • 2
    • 3

    4、时间的结束

    import dayjs from "dayjs";
    console.log("=====>", dayjs().endOf("year").format("YYYY-MM-DD HH:mm:ss"));
    // =====> 2021-12-31 23:59:59
    
    • 1
    • 2
    • 3

    四、总结

    dayjs 是一个轻量的处理时间和日期的库,它的 API 设计的非常简单,使用起来也非常方便,如果你的项目中需要处理时间和日期,那么 dayjs 是一个不错的选择。更多的 API 可以参考官方文档:https://dayjs.fenxianglu.cn/

  • 相关阅读:
    详解 Go 语言中的 init () 函数
    监控平台设计 之 Prometheus 存储 LevelDB
    JavaIO流04:对象流、随机存取文件流(RandomAccessFile类)
    如何去云服务器申请环境跑深度学习模型
    蛋白质结构预测 AlphaFold2、ColabFold、Uni-Fold
    Nginx代理中使用斜杠的区别
    【数学建模】——【新手小白到国奖选手】——【学习路线】
    全栈自动化测试python基础之文件的操作
    LNMP架构&部署Discuz论坛系统
    刷爆LeetCode 字节技术官亲码算法面试进阶神技太香了
  • 原文地址:https://blog.csdn.net/qq_29689343/article/details/133171953
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号