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

免备案域名是危险网站网站建设提供资料

免备案域名是危险网站,网站建设提供资料,房地产建筑设计公司,智慧团建登录平台入口简介 Day.js 是一个轻量级的 JavaScript 日期库,它提供了简单易用的 API 来处理日期和时间。以及更加轻量级,并且具有更快的性能。 安装 npm install dayjs 使用 import dayjs from "dayjs";dayjs().format("YYYY-MM-DD HH:mm:ss&qu…

简介

Day.js 是一个轻量级的 JavaScript 日期库,它提供了简单易用的 API 来处理日期和时间。以及更加轻量级,并且具有更快的性能。

安装

npm install dayjs

使用

import dayjs from "dayjs";dayjs().format("YYYY-MM-DD HH:mm:ss");

经典场景

1. 获取当前时间

dayjs().format("YYYY-MM-DD HH:mm:ss");

2. 格式化时间

dayjs("2021-01-01").format("YYYY-MM-DD HH:mm:ss");

3. 解析时间

dayjs("2021-01-01 12:00:00", "YYYY-MM-DD HH:mm:ss");

4. 时间加减

dayjs().add(1, "day").format("YYYY-MM-DD HH:mm:ss");
dayjs().subtract(1, "day").format("YYYY-MM-DD HH:mm:ss");

5. 时间比较

dayjs("2021-01-01").isBefore("2021-01-02");
dayjs("2021-01-01").isAfter("2021-01-02");
dayjs("2021-01-01").isSame("2021-01-01");

6. 获取时间差

dayjs("2021-01-01").diff("2021-01-02", "day");
类型说明
year
quarter季度
month
week
day
hour小时
minute分钟
second
millisecond毫秒

diff 默认返回是整数,如果需要返回小数,可以传入第三个参数:true|false。

7. 快速获取常用日期

# 前一天
dayjs().subtract(1, "day").format("YYYY-MM-DD HH:mm:ss");
# 后一天
dayjs().add(1, "day").format("YYYY-MM-DD HH:mm:ss");
# 前一周
dayjs().subtract(1, "week").format("YYYY-MM-DD HH:mm:ss");
# 后一周
dayjs().add(1, "week").format("YYYY-MM-DD HH:mm:ss");
# 前一月
dayjs().subtract(1, "month").format("YYYY-MM-DD HH:mm:ss");
# 后一月
dayjs().add(1, "month").format("YYYY-MM-DD HH:mm:ss");
# 前一季
dayjs().subtract(1, "quarter").format("YYYY-MM-DD HH:mm:ss");
# 后一季
dayjs().add(1, "quarter").format("YYYY-MM-DD HH:mm:ss");
# 前一年
dayjs().subtract(1, "year").format("YYYY-MM-DD HH:mm:ss");
# 后一年
dayjs().add(1, "year").format("YYYY-MM-DD HH:mm:ss");

8. 日期范围

import dayjs from "dayjs";
import isBetween from "dayjs/plugin/isBetween";dayjs.extend(isBetween);// 是否属于某个范围
dayjs("2021-01-01").isBetween("2021-01-01", "2021-01-02");
// 是否属于某个范围(包含边界)
// [ 包含开始日期,包含结束日期 ]
// ( 不包含开始日期,不包含结束日期 )
dayjs("2021-01-01").isBetween("2021-01-01", "2021-01-02", "day", "[)");

9. 倒计时实现

import { useState, useEffect, useCallback, useRef } from "react";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";dayjs.extend(duration);/*** 倒计时 Hook* @param {number} targetTime - 目标时间戳(毫秒)* @param {number} interval - 更新间隔(毫秒),默认 1000ms* @returns {Object} 倒计时状态* @returns {number} remainingTime - 剩余时间(毫秒)* @returns {boolean} isFinished - 是否结束* @returns {Object} formattedTime - 格式化后的时间对象* @returns {Function} reset - 重置倒计时*/
export default function useCountdown(targetTime, interval = 1000) {const [remainingTime, setRemainingTime] = useState(0);const [isFinished, setIsFinished] = useState(false);const targetTimeRef = useRef(targetTime);// 计算剩余时间const calculateRemainingTime = useCallback(() => {const now = dayjs();const target = dayjs(targetTimeRef.current);const diff = target.diff(now);if (diff <= 0) {setRemainingTime(0);setIsFinished(true);return;}setRemainingTime(diff);setIsFinished(false);}, []);// 格式化时间const formatTime = useCallback((time) => {const duration = dayjs.duration(time);return {days: Math.floor(duration.asDays()),hours: duration.hours(),minutes: duration.minutes(),seconds: duration.seconds(),milliseconds: duration.milliseconds(),};}, []);// 重置倒计时const reset = useCallback(() => {// 更新目标时间引用targetTimeRef.current = dayjs().add(24, "hour").valueOf();setIsFinished(false);calculateRemainingTime();}, [calculateRemainingTime]);useEffect(() => {// 初始化目标时间targetTimeRef.current = targetTime;// 初始化calculateRemainingTime();// 设置定时器const timer = setInterval(() => {calculateRemainingTime();}, interval);// 清理定时器return () => clearInterval(timer);}, [calculateRemainingTime, interval, targetTime]);return {remainingTime,isFinished,formattedTime: formatTime(remainingTime),reset,};
}// hook使用
const { formattedTime, isFinished, reset } = useCountdown(targetTime);

10. 日历组件实现

import { useState, useMemo } from "react";
import dayjs from "dayjs";
import weekOfYear from "dayjs/plugin/weekOfYear";
import isoWeek from "dayjs/plugin/isoWeek";dayjs.extend(weekOfYear);
dayjs.extend(isoWeek);export default function Calendar() {const [currentDate, setCurrentDate] = useState(dayjs());const [selectedDate, setSelectedDate] = useState(dayjs());// 生成日历数据const calendarData = useMemo(() => {const firstDayOfMonth = currentDate.startOf("month");const lastDayOfMonth = currentDate.endOf("month");const firstDayOfWeek = firstDayOfMonth.day();const daysInMonth = lastDayOfMonth.date();const daysInPrevMonth = firstDayOfMonth.subtract(1, "month").daysInMonth();const days = [];// 上个月的天数for (let i = firstDayOfWeek - 1; i >= 0; i--) {days.push({date: firstDayOfMonth.subtract(i + 1, "day"),isCurrentMonth: false,});}// 当前月的天数for (let i = 1; i <= daysInMonth; i++) {days.push({date: currentDate.date(i),isCurrentMonth: true,});}// 下个月的天数const remainingDays = 42 - days.length; // 6行7列for (let i = 1; i <= remainingDays; i++) {days.push({date: lastDayOfMonth.add(i, "day"),isCurrentMonth: false,});}return days;}, [currentDate]);// 切换月份const changeMonth = (delta) => {setCurrentDate(currentDate.add(delta, "month"));};// 切换年份const changeYear = (delta) => {setCurrentDate(currentDate.add(delta, "year"));};// 判断是否是今天const isToday = (date) => {return date.isSame(dayjs(), "day");};// 判断是否是选中日期const isSelected = (date) => {return date.isSame(selectedDate, "day");};return (<div className="w-full max-w-md mx-auto bg-white rounded-lg shadow-lg p-4">{/* 日历头部 */}<div className="flex items-center justify-between mb-4"><div className="flex items-center space-x-2"><buttononClick={() => changeYear(-1)}className="p-2 hover:bg-blue-50 rounded-full transition-colors">{"<<"}</button><buttononClick={() => changeMonth(-1)}className="p-2 hover:bg-blue-50 rounded-full transition-colors">{"<"}</button></div><div className="text-lg font-semibold text-blue-600">{currentDate.format("YYYY年 MM月")}</div><div className="flex items-center space-x-2"><buttononClick={() => changeMonth(1)}className="p-2 hover:bg-blue-50 rounded-full transition-colors">{">"}</button><buttononClick={() => changeYear(1)}className="p-2 hover:bg-blue-50 rounded-full transition-colors">{">>"}</button></div></div>{/* 星期标题 */}<div className="grid grid-cols-7 gap-1 mb-2">{["日", "一", "二", "三", "四", "五", "六"].map((day) => (<divkey={day}className="text-center text-sm font-medium text-blue-600 py-2">{day}</div>))}</div>{/* 日历主体 */}<div className="grid grid-cols-7 gap-1">{calendarData.map(({ date, isCurrentMonth }, index) => (<buttonkey={index}onClick={() => setSelectedDate(date)}className={`aspect-square p-1 text-sm rounded-lg transition-colors${isCurrentMonth? "hover:bg-blue-50": "text-gray-400 hover:bg-gray-50"}${isToday(date) ? "bg-blue-100" : ""}${isSelected(date)? "bg-blue-500 text-white hover:bg-blue-600": ""}`}>{date.date()}</button>))}</div></div>);
}

11.国际化

import dayjs from "dayjs";
import "dayjs/locale/zh-cn";dayjs.locale("zh-cn");
dayjs().format("YYYY-MM-DD HH:mm:ss");

 更多用法


文章转载自:

http://ferj8MBx.ydwnc.cn
http://Q7Pya98j.ydwnc.cn
http://RHUoJVZh.ydwnc.cn
http://AEcRvguf.ydwnc.cn
http://gVGnDpZ4.ydwnc.cn
http://iOml95yv.ydwnc.cn
http://AQFxxyDV.ydwnc.cn
http://rFamvhyk.ydwnc.cn
http://BdRoQmfu.ydwnc.cn
http://NAT1ADMd.ydwnc.cn
http://jlBkK62F.ydwnc.cn
http://oUCfEGH8.ydwnc.cn
http://co2ltGuQ.ydwnc.cn
http://GeyOvTMd.ydwnc.cn
http://fFB6dwCh.ydwnc.cn
http://6Go1O6lI.ydwnc.cn
http://esygr5lU.ydwnc.cn
http://ruanb1Tj.ydwnc.cn
http://OWVH8z1A.ydwnc.cn
http://vc8C73jn.ydwnc.cn
http://fdvjBgr2.ydwnc.cn
http://qYSLXsOV.ydwnc.cn
http://nTdxzmEP.ydwnc.cn
http://6JIDOfrx.ydwnc.cn
http://0JeI1eDD.ydwnc.cn
http://hown4ftH.ydwnc.cn
http://BVrl6CNb.ydwnc.cn
http://0bvJKb4V.ydwnc.cn
http://lnQM97Ye.ydwnc.cn
http://aAiZASmj.ydwnc.cn
http://www.dtcms.com/wzjs/757644.html

相关文章:

  • 商务网站设计与开发吉林市网站建设公司
  • 网站建站的步骤企业邮箱要收费吗
  • 网站排名在线优化工具wordpress 汉化 插件怎么用
  • 深圳可以做网站的公司校园网站开发的需求分析
  • 用户研究网站河北移动端网站建设
  • wordpress手机站主题有哪些推广平台和渠道
  • 教育机构电商网站建设加盟标志设计作业
  • 前几年做那个网站能致富做长尾词优化去哪些网站
  • 成都市建设监理协会网站wordpress前端是什么
  • 苏州网站设计价格重庆平台网站建设找哪家
  • 网站上的图片格式怎么做wordpress 不能更新
  • 郑州市建设局网站wordpress图片命名
  • 做图海报网站旅游网站规划设计方案
  • 做网站困难嘛想自己做个网站怎么做
  • 写出网站建设的基本流程山东ui设计培训班
  • 网站建设意义和作用网站自助建站开发制作
  • 人防工程做资料的网站带后台的网站模板
  • 长春网站开发招聘杭州建设信用平台
  • 为网站网站做推广深圳seo搜索优化
  • 哪个网站可以接广告做做单页网站需要做什么
  • 三明市住房和城乡建设局网站网站开发时间表
  • 做模板网站贵阳市网站做的最好的
  • 物流网站首页图片wordpress的slider
  • 进度跟踪网站开发做电商网站的流程
  • 成都免费建网站公司wordpress 翻译函数
  • 怎样建设淘客网站青岛互联网设计公司
  • 简洁的个人网站淘宝提货网站怎么做的
  • 学网站开发哪里好90做网站
  • 嘉兴装修公司做网站郑州小程序制作流程及费用
  • 深圳网站建设建设wordpress 买域名