Day.js 常用方法

Day.js是一个小巧的JavaScript库,用于处理日期和时间。它提供了解析、验证、格式化、加减日期等功能,如初始化日期、格式化日期时间、加减日期时间、获取月份的第一天和最后一天等。此外,还包括判断日期前后关系、是否相等以及是否在两个日期之间的方法。
摘要由CSDN通过智能技术生成

Day.js是一个极简的JavaScript库,可以为现代浏览器解析、验证、操作和显示日期和时间,文件大小只有2KB左右,下载、解析和执行的JavaScript更少。

官网:Day.js中文网

1. 安装

npm install dayjs --save

2.引入 

3. 初始化日期 / 时间 

dayjs().format('YYYY-MM-DD');		    // 初始化日期
dayjs().format('YYYY-MM-DD HH:mm:ss');  // 初始化日期时间

4. 格式化日期 / 时间 

dayjs(value).format('YYYY-MM-DD');			// 初始化日期
dayjs(value).format('YYYY-MM-DD HH:mm:ss'); // 初始化日期时间

5.加 / 减 

dayjs().add / dayjs().subtract 代表在当前时间上去加减;
dayjs(value).add / dayjs(value).subtract 代表在指定时间(value)上去加减;

dayjs().add(7, 'day').format('YYYY-MM-DD');   // 2022-04-27 今天(2022-04-20)加上7天
dayjs().add(1, 'month').format('YYYY-MM-DD');   // 2022-05-20 今天(2022-04-20)加上一月

dayjs().subtract(2, 'year').format('YYYY-MM-DD');   // 2020-05-20 今天(2022-04-20)减去2年
dayjs().subtract(2, 'hour').format('YYYY-MM-DD HH:mm:ss');   // 2022-04-20 14:03:39 今天现在(2022-04-20 16:03:39)减去2小时

 

6. 获取某年某月的第一天或最后一天

 获取某年某月的第一天

dayjs().startOf('year').format('YYYY-MM-DD HH:mm:ss')   // 2022-01-01 00:00:00  => 第一天格式化出来的时分秒都是0
dayjs().startOf('month').format('YYYY-MM-DD')   // 2022-04-01

获取某年某月的最后一天: 

dayjs().endOf('year').format('YYYY-MM-DD HH:mm:ss')   // 2022-12-31 23:59:59  => 最后时间 格式化出来的时分秒是23:59:59
dayjs().endOf('month').format('YYYY-MM-DD')   // 2022-04-30

7. 获取星期几
dayjs().day() : 返回0(星期日)到6(星期六)的数字

设置时也只能接受 0-6 的数字:
dayjs().day(6).format('YYYY-MM-DD'):获取最近周六的日期 => 2022-04-23
dayjs().day(0).format('YYYY-MM-DD'):获取最近周日的日期 => 2022-04-17

8. 获取毫秒数
dayjs('2019-01-25').valueOf() 或 dayjs().valueOf()

9. 获取时间差(默认输出的差值单位是毫秒)

dayjs('2019-01-25').diff('2018-06-05', 'month');         // 7
dayjs('2019-01-25').diff(dayjs('2018-06-05'), 'month');  // 7

 10.获取时、分、秒

    console.log('-----获取年', dayjs().year());    // ==>> 2022
    console.log('-----获取月', dayjs().month());   // 0到11的数字 ==>> 3
    console.log('-----获取星期', dayjs().day());   // 0(星期日)到6(星期六)的数字 ==>> 3
    console.log('-----获取天', dayjs().date());    // 1到31的数字 ==>> 20
    console.log('-----获取小时', dayjs().hour());  // 0到23的数字 ==>> 16
    console.log('-----获取分钟', dayjs().minute());// 0到59的数字 ==>> 55
    console.log('-----获取秒', dayjs().second());  // 0到59的数字 ==>> 55
    console.log('-----获取毫秒', dayjs().millisecond());  // 0到999的数字 ==>> 333

 11. 将毫秒转为时分秒

// 下面毫秒数代表:2022-04-20 17:43:20
const timestr = 1650447800731;   // 毫秒值必须是number类型,如果是string,结果可能和你想的不一样
console.log('将毫秒转为年-月-日 时:分:秒', dayjs(timestr).format('YYYY-MM-DD HH:mm:ss'));
console.log('获取年', dayjs(timestr).year()); // 
console.log('获取月', dayjs(timestr).month());
console.log('获取天', dayjs(timestr).date());
console.log('获取时', dayjs(timestr).hour());
console.log('获取分', dayjs(timestr).minute());

12.判断一个日期是否在另外一个日期之后 isAfter

// day.js 为 2022-04-20
console.log('isAfter', dayjs().isAfter(dayjs('2011-01-01'))) 			 	// true
console.log('isAfter', dayjs('2022-04-20').isAfter(dayjs('2022-04-21')))	// false
console.log('isAfter', dayjs('2022-04-20').isAfter(dayjs('2022-04-20')))	// 相同也为false

13.判断一个日期是否在另外一个日期之前 isBefore

// day.js 为 2022-04-20
console.log('isBefore', dayjs().isBefore(dayjs('2011-01-01')))             // false
console.log('isBefore', dayjs('2022-04-20').isBefore(dayjs('2022-04-21'))) // true
console.log('isBefore', dayjs('2022-04-20').isBefore(dayjs('2022-04-20'))) // 日期相同时也为false

14.判断两个日期是否相同 isSame

// day.js 为 2022-04-20
console.log('isSame', dayjs().isSame(dayjs('2011-01-01')))  			// false
console.log('isSame', dayjs('2022-04-20').isSame(dayjs('2022-04-21')))	// false
console.log('isSame', dayjs('2022-04-20').isSame(dayjs('2022-04-20')))	// true

15.判断一个日期是否在两个日期之间 isBetween

注意:
此功能依赖IsBetween插件

import dayjs from 'dayjs'	// 引入dayjs
import isBetween from 'dayjs/plugin/isBetween'	// 引入相关插件

created() {
   dayjs.extend(isBetween); // 挂载插件
   // 使用插件
   console.log('isBetween', dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25')) )
}

 

### 回答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、付费专栏及课程。

余额充值