day.js常用方法

一、时间格式

Tue Mar 28 2023 17:26:39 GMT+0800  //(中国标准时间)
2021-07-29T21:35:54+08:00  //末尾存在+,代表时间格式为包括时区的时间格式,+08:00代表东八区
2021-07-29T21:35:54Z  //末尾有Z的,为ISO格式的时间,代表UTC时间(UTC:世界标准时间,即格林威治标准时间,初中学的本初子午线),不带时区,假如中国地区(东八区)的去看这个时间要再加8h

二、dayjs()格式化

dayjs()等价于dayjs(Date.now())、dayjs(new Date())
获取到的时间格式为:Tue Mar 28 2023 17:26:39 GMT+0800 (中国标准时间)
dayjs()对象格式化:
格式化dayjs()对象为YYYY-MM-DD HH:mm:ss格式

dayjs (时间).format('YYYY-MM-DD HH:mm:ss')

三、dayjs()获取年月日时分秒

    console.log("dayjs().get('year'):", dayjs().get("year")); //年 [1,366]
    console.log("dayjs().get('month'):", dayjs().get("month")); //月 [0,11] 0表示1月
    console.log("dayjs().get('date'):", dayjs().get("date")); //日[1,31]
    console.log("dayjs().get('hour'):", dayjs().get("hour")); //时 [0,23]
    console.log("dayjs().get('minute'):", dayjs().get("minute")); //分 [0,59]
    console.log("dayjs().get('second'):", dayjs().get("second")); //秒 [0,59]
    console.log("dayjs().get('millisecond'):", dayjs().get("millisecond")); //毫秒[0,999]
    console.log("dayjs().get('day'):", dayjs().get("day")); //星期几 [0,6]。0(星期日)到6(星期六)

四、dayjs()计算

加减指定时间

dayjs().add(3,”year”)
dayjs().subtract(5,”minute”)

计算差值

let time1 = "2023-03-28 14:28:04"
let time2 = "2022-04-15 12:05:58"
Time2.diff(time,”hour”)     //相差多少小时
Time2.diff(time1,”minute”)  //相加多少分钟

五、dayjs()判断

判断大小

console.log("当前时间:",dayjs().format("YYYY-MM-DD"))
console.log("当前时间< 2022-01-01 吗):",dayjs().isBefore(dayjs('2022-01-01')))
console.log("当前时间 > 2022-01-01 吗):",dayjs().isAfter(dayjs('2022-01-01')))
console.log("当前时间 = 222-01-01 吗):",dayjs().isSame(dayjs('2022-01-01')))

判断是否在两数之间

import dayjs from "dayjs"
import isBetween from "dayjs/plugin/isBetween"
dayjs.extend(isBetween);

六、安装

安装:npm i -S dayjs
局部引入:import dayjs from "dayjs";

报错:TypeError: _ctx.dayjs is not a function

app.config.globalProperties.$dayjs = dayjs  // 全局引入,原型挂载
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Auto.js是一款能够模拟人的操作来自动化手机操作的工具。获取当前时间也是Auto.js的基本功能之一。可以使用JavaScript中的Date对象来获取当前时间。具体实现的代码如下: var now = new Date(); //获取当前时间 var year = now.getFullYear(); //获取年份 var month = now.getMonth() + 1; //获取月份,注意月份从0开始计算,所以要加1 var day = now.getDate(); //获取日期 var hour = now.getHours(); //获取小时 var minute = now.getMinutes(); //获取分钟 var second = now.getSeconds(); //获取秒钟 以上代码将获取当前年月日时分秒的数据存储在对应变量中,可以根据实际需要进行使用。同时,Auto.js也提供了更加简便的方法获取时间,例如: var date = new Date().toLocaleDateString(); //获取当前日期,格式为“2019/10/1” var time = new Date().toLocaleTimeString(); //获取当前时间,格式为“下午/上午 11:01:12” 需要注意的是,获取当前时间时要确保手机时间设置正确,否则获取到的时间将会是不正确的。另外,Auto.js还有一些常用的时间操作方法,例如倒计时、定时器等,可以根据实际需要进行学习和使用。 ### 回答2: Auto.js是一款高效的Android自动化测试工具,可以实现多种自动化任务,比如模拟点击、输入、滑动屏幕等。Auto.js中可以使用JavaScript语言进行编程,通过代码来实现自动化任务。而获取当前时间也是Auto.js常用的功能之一。 获取当前时间的方法有多种,比如调用系统的方法获取当前时间、通过时间戳获取时间等。下面我们以获取当前时间的方式为例,来介绍Auto.js中如何获取当前时间。 获取当前时间可以使用JavaScript中的Date对象,代码示例如下: ```javascript var currentDate = new Date(); ``` 上述代码将创建一个Date对象,该对象中包含了当前的年、月、日、时、分、秒等信息,可以通过该对象来获取对应的时间。 获取当前的年、月、日信息可以使用以下代码: ```javascript var year = currentDate.getFullYear(); //获取当前的年份 var month = currentDate.getMonth() + 1; //获取当前的月份,需要加1 var day = currentDate.getDate(); //获取当前的日期 ``` 获取当前的小时、分钟、秒信息可以使用以下代码: ```javascript var hour = currentDate.getHours(); //获取当前的小时 var minute = currentDate.getMinutes(); //获取当前的分钟 var second = currentDate.getSeconds(); //获取当前的秒数 ``` 上述代码通过调用Date对象的方法获取当前的年、月、日、时、分、秒信息,从而实现了获取当前时间的功能。例如,完整的代码如下: ```javascript var currentDate = new Date(); var year = currentDate.getFullYear(); //获取当前的年份 var month = currentDate.getMonth() + 1; //获取当前的月份,需要加1 var day = currentDate.getDate(); //获取当前的日期 var hour = currentDate.getHours(); //获取当前的小时 var minute = currentDate.getMinutes(); //获取当前的分钟 var second = currentDate.getSeconds(); //获取当前的秒数 var currentTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; toast(currentTime); //显示当前时间 ``` 上述代码将获取到的时间信息拼接成了一个字符串,并使用toast方法来显示当前时间。通过上述代码,我们可以轻松地获取到当前的时间信息,方便Auto.js中的自动化任务编写。 ### 回答3: Auto.js是Android平台上的一款自动化测试工具,它可以通过JavaScript脚本编写自动化任务。获取当前时间是我们经常用到的功能之一,Auto.js也提供了相应的API来实现。 在Auto.js中,可以使用Date对象获取当前时间。具体的代码如下: ```javascript var now = new Date(); var year = now.getFullYear(); //获取完整的年份(4位,1970-????) var month = now.getMonth() + 1; //获取当前月份(0-11,0代表1月) var day = now.getDate(); //获取当前日(1-31) var hour = now.getHours(); //获取当前小时数(0-23) var minute = now.getMinutes(); //获取当前分钟数(0-59) var second = now.getSeconds(); //获取当前秒数(0-59) var millisecond = now.getMilliseconds(); //获取当前毫秒数(0-999) ``` 以上代码中,通过new Date()获取当前时间对象,然后使用对象的各个方法获取对应的时间信息。需要注意的是,月份的获取需要加1,因为JavaScript中月份从0开始表示。 除了使用Date对象获取当前时间外,Auto.js还提供了一些其他的时间相关API,比如sleep和setTimeInMillis等方法。使用这些方法可以更加灵活地控制时间,实现更多复杂的自动化任务。 总的来说,Auto.js获取当前时间的方法非常简单,只需要使用Date对象即可。结合其他时间相关API的使用,可以实现更多有趣的自动化任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值