一个包含44的方法的日期时间JS库,支持获取本月最后一天、本月第一天、本周、上周、下周等日期

插件介绍

  • timer库支持获取当前日期时间、时间差、延迟执行、自定义多个轮询等功能

插件特色

  • 无论项目多大,全局仅仅只需要一个定时器,减少内存消耗,防止定时器过多导致的系统崩溃
下载地址
  • https://gitee.com/xbc-it/plugins/tree/master/xbc-timer

交流沟通

加群交流QQ群:1078718987

引入SDK

import timer from './static/timer.js'

内置方法

1、start(reset) 开启定时任务

reset 布尔值,是否开启单页任务,即进入新页面时,重置所有定时任务,默认true,即开启

timer.start()
2、remove(taskId) 移除指定任务
// 移除指定单个任务
timer.remove('test')
// 移除指定多个任务
timer.remove(['test1', 'test2'])
3、clear() 移除所有任务
timer.clear()
4、delay(tasks) 延时任务链(每个任务仅执行一次)

tasks 要延时执行的任务,支持单个(JSON),也支持同时多个(jsonArray),包含的参数有:
func-任务名(特别说明,不能加括号)
delay-延迟执行时间,单位毫秒
param-任务参数,支持合法的格式

// 执行单个延时任务
timer.delay({
	func: this.test,
	delay: 5000
})
// 执行多个延时任务
timer.delay([
	{	// 延迟5秒执行
		func: this.test1,
		delay: 5000
	},
	{	// 延迟10秒执行
		func: this.test2,
		delay: 10000
	}
])
5、chain(tasks) 定时任务链(每个任务按照指定间隔循环执行)

tasks 要延时执行的任务,支持单个(JSON),也支持同时多个(jsonArray),包含的参数有:
id-任务ID
func-任务名(特别说明,不能加括号)
delay-延迟执行时间,单位毫秒
param-任务参数,支持合法的格式
interval-间隔执行时间,单位毫秒,默认1000
time-定时执行时间,支持yyyy-mm-dd hh:mm:ss、yyyy-mm-dd、hh:mm:ss

// 同时添加多个循环任务
timer.chain([
	{
		id: 'test1',
		func: this.test1,
		delay: 2000,
		interval: 1000,
		param: 5
	},
	{
		id: 'test2',
		func: this.test2,
		interval: 1000,
		param: {name: 'test'}
	}
])
// 同时添加多个定时任务
timer.chain([
	{	// 在指定的某个具体时间定时执行(年月日时分秒均须固定2位)
		id: 'test1',
		func: this.test1,
		param: 5,
		time: '2021-05-22 12:00:00'
	},
	{	// 在指定的某个日期的00:00:00定时执行(不传入时间时,默认00:00:00)
		id: 'test2',
		func: this.test2,
		time: '2021-05-22'
	},
	{	// 在每天的某个时间点定时执行(时分秒均须固定2位)
		id: 'test3',
		func: this.test3,
		time: '00:30:20'
	}
])
6、getYearDates(year=0, bool=true, separ=’-’) 获取指定年月内的日期

year 整数值,年份(4位),当前年份时传入0
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getYearDates()
7、getYearMonthDates(year, month, isAll=false, bool=true, separ=’-’) 获取指定年月内的日期

year 整数值,年份(4位)
month 整数值,月份
isAll 布尔值,是否返回指定年月内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getYearMonthDates(2021, 11)
8、getThisMonthDates(isAll=false, bool=true, separ=’-’) 获取本月内的日期

isAll 布尔值,是否返回本月内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getThisMonthDates()
9、getLastMonthDates(isAll=false, bool=true, separ=’-’) 获取上月内的日期

isAll 布尔值,是否返回上月内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getLastMonthDates()
10、getNextMonthDates(isAll=false, bool=true, separ=’-’) 获取下月内的日期

isAll 布尔值,是否返回下月内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getNextMonthDates()
11、getRangeWeekDates(step=0, isAll=false, bool=true, separ=’-’) 获取向前向后推的X周内的日期

step 整数值,表当向前向后推的多少周,本周时为0
isAll 布尔值,是否返回本周内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getRangeWeekDates()
12、getThisWeekDates(isAll=false, bool=true, separ=’-’) 获取本周日期

isAll 布尔值,是否返回本周内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getThisWeekDates()
13、getLastWeekDates(isAll=false, bool=true, separ=’-’) 获取上周日期

isAll 布尔值,是否返回上周内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getLastWeekDates()
14、getNextWeekDates(isAll=false, bool=true, separ=’-’) 获取下周日期

isAll 布尔值,是否返回下周内的所有连续日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getNextWeekDates()
15、getYesterday(bool=true, separ=’-’) 获取昨天日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 年月日分隔,默认-,设置为text时为年月日

timer.getYesterday()
16、getToday(bool=true, separ=’-’) 获取今天日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 年月日分隔,默认-,设置为text时为年月日

timer.getToday()
17、getYearMonthLastDate(year=0, month=0, bool=true, separ=’-’) 获取指定年月的最后一天的日期

year 整数值,年份(4位),当前年份时传入0
month 整数值,月份,当前月份时传入0
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getYearMonthLastDate()
18、getThisMonthLastDate(bool=true, separ=’-’) 获取本月最后一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getThisMonthLastDate()
19、getLastMonthLastDate(bool=true, separ=’-’) 获取上月最后一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getLastMonthLastDate()
20、getNextMonthLastDate(bool=true, separ=’-’) 获取下月最后一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getNextMonthLastDate()
21、getRangeWeekLastDate(step=0, bool=true, separ=’-’) 获取向前向后推的X周最后一天的日期

step INT,获取向前向后的X周,0-本周,1-下周,2-后2周,-1-上周,-2-上2周
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getRangeWeekLastDate()
22、getThisWeekLastDate(bool=true, separ=’-’) 获取本周最后一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getThisWeekLastDate()
23、getLastWeekLastDate(bool=true, separ=’-’) 获取上周最后一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getLastWeekLastDate()
24、getNextWeekLastDate(bool=true, separ=’-’) 获取下周最后一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getNextWeekLastDate()
25、getRangeWeekFirstDate(step=0, bool=true, separ=’-’) 获取向前向后推的X周第一天的日期

step INT,获取向前向后的X周,0-本周,1-下周,2-后2周,-1-上周,-2-上2周
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getRangeWeekFirstDate()
26、getThisWeekFirstDate(bool=true, separ=’-’) 获取本周第一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getThisWeekFirstDate()
27、getLastWeekFirstDate(bool=true, separ=’-’) 获取上周第一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getLastWeekFirstDate()
28、getNextWeekFirstDate(bool=true, separ=’-’) 获取下周第一天的日期

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getNextWeekFirstDate()
29、getLatestDates(days=7, isAll=false, bool=true, separ=’-’) 获取最近几天内的日期

days 整数,最近几天,默认7天
isAll 布尔值,是否显示所有日期,默认否
bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 字符串,年月日分隔,默认-,设置为text时为年月日

timer.getLatestDates()
30、getYear(bool) 获取当前年份

bool 布尔值,是否获取完整年份,true-是(默认),false-否

timer.getYear()
31、getMonth(bool) 获取当前月份

bool 布尔值,是否固定月份为2位,true-是(默认),false-否

timer.getMonth()
32、getDate(bool) 获取当前日期

bool 布尔值,是否固定日期为2位,true-是(默认),false-否

timer.getDate()
33、getYearMonth(bool, separ) 获取当前年月

bool 布尔值,是否设置月份固定2位,true-是(默认),false-否
separ 年月之间的分隔,默认-

timer.getYearMonth()
34、getYearMonthDate(bool, separ) 获取当前年月日

bool 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ 年月日分隔,默认-

timer.getYearMonthDate()
35、getDateTime(bool1, separ1, bool2, separ2) 获取当前日期时间

bool1 布尔值,是否设置月日固定2位,true-是(默认),false-否
separ1 年月日分隔,默认-
bool2 布尔值,是否设置时分秒固定2位,true-是(默认),false-否
separ3 时分秒之间的分隔,默认:

timer.getDateTime()
36、getTime(bool, separ) 获取当前时分秒

bool 布尔值,是否设置时分秒固定2位,true-是(默认),false-否
separ 时分秒之间的分隔,默认:

timer.getTime()
37、getHours(bool) 获取当前小时数

bool 布尔值,是否固定小时数为2位,true-是(默认),false-否

timer.getHours()
38、getMinutes(bool) 获取当前分钟数

bool 布尔值,是否固定分钟数为2位,true-是(默认),false-否

timer.getMinutes()
39、getSeconds(bool) 获取当前秒数

bool 布尔值,是否固定秒数为2位,true-是(默认),false-否

timer.getSeconds()
40、getStamp(bool) 获取当前时间戳,单位为毫秒

bool 布尔值,设置单位是否为毫秒,默认是,false时返回的时间戳单位为秒

timer.getStamp()
41、getWeek(type) 获取当前星期几

type 返回的星期格式,可选值有:
0-周x,默认
1-星期x
2-礼拜x
3-英文
4-英文简写

timer.getWeek()
42、getDiff(timestamp) 获取某个时间距离当前时间多久

timestamp 时间戳,支持秒和毫秒
return 刚刚、x分钟前、x小时前、x天前、年-月-日

timer.getDiff('1621700428560')
43、roll(param) 数字自增长

param.number: 增加到的目标值,默认为0
param.step: 增加的步长值,默认为1
param.start: 初始值,默认为0
param.speed: 变化的时间,单位毫秒,默认10毫秒
param.formate: 是否开启数值千分位,默认为false
param.change: 监听数值增值变化

timer.roll({
	number: 100,
	step: 1,
	change: number=>{
		console.log(number)
	}
});
44、countdown(param) 倒计时

param.id: 唯一标识符
param.seconds: 倒计时时间,单位秒
param.change: 倒计时回调
param.done: 倒计时结束回调

timer.countdown({
	id: 'test',
	seconds: 36,
	change: res=>{
		this.time = `${res.days}${res.hours}${res.minutes}${res.seconds}`
	},
	done: ()=>{
		console.log('倒计时结束')
	}
});

支持的方法有:pause()、start(seconds)、restart(seconds)

  1. pause() 暂停当前倒计时
oTimer.pause();
  1. start(seconds) 开始当前倒计时
    seconds: 可选,单位为秒,有值时,会从当前值开始倒计时
// 从当前暂停时间开始继续倒计时
oTimer.start();

// 从200秒开始倒计时
oTimer.start(200);
  1. restart(seconds) 重新开始倒计时
    seconds: 可选,单位为秒,有值时,会从当前值重新开始倒计时
// 从默认开始时间重新倒计时
oTimer.restart();

// 从200秒重新倒计时
oTimer.restart(200);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT男有个IT梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值