Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】

这篇具有很好参考价值的文章主要介绍了Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 界面实现

 <input id="Date_select" type="text" class="form-control" placeholder="请选择时间范围" style="border-radius: 4px;" />
 <input id="StartDate" type="hidden" />
 <input id="EndDate" type="hidden" />

2. JS具体实现

2.1 第一种实现

//时间选择
layui.laydate.render({
	elem: '#Date_select',
    type: 'date',
	range: true,
	shortcuts: [
		{
			text: '本周',
			value: function () {
				let now = new Date(); //当前日期 
				let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天,周日为0,但实际上平时都是以周一为一周的开始,所以周3因该为2
				let nowDay = now.getDate(); //当前日 
				let nowMonth = now.getMonth(); //当前月 
				let nowYear = now.getFullYear(); //当前年 
				let startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
				let endTime = new Date(nowYear, nowMonth, nowDay + 6 - nowDayOfWeek);
				var value = [];
				value.push(startTime);
				value.push(endTime);
				return value;
			}()
		},
		{
			text: "本月",
			value: function () {
				let now = new Date(); //当前日期 
				let nowMonth = now.getMonth(); //当前月 
				let nowYear = now.getFullYear(); //当前年 
				let startTime = new Date(nowYear, nowMonth, 1);
				let endTime = new Date(nowYear, nowMonth + 1, 0);
				var value = [];
				value.push(startTime);
				value.push(endTime);
				return value;
			}()
		},
		{
			text: "本季度",
			value: function () {
				let now = new Date(); //当前日期 
				let nowMonth = now.getMonth(); //当前月 
				let nowYear = now.getFullYear(); //当前年 
				let jd = Math.ceil((nowMonth + 1) / 3);
				let startTime = new Date(nowYear, (jd - 1) * 3, 1);
				let endTime = new Date(nowYear, jd * 3, 0);
				var value = [];
				value.push(startTime);
				value.push(endTime);
				return value;
			}()
		},
		{
			text: "本年",
			value: function () {
				let now = new Date(); //当前日期 
				let nowYear = now.getFullYear(); //当前年 
				let startTime = new Date(nowYear, 0, 1);
				let endTime = new Date(nowYear, 11, 31);
				var value = [];
				value.push(startTime);
				value.push(endTime);
				return value;
			}()
		},
	],
	done: function (value, date, endDate) {//选择后事件
		var StartDate = null;
		var EndDate = null;
		if (!!value) {
			StartDate = `${date.year}-${date.month}-${date.date} 00:00:00`;
			EndDate = `${endDate.year}-${endDate.month}-${endDate.date} 23:59:59`;
		}
		$("#StartDate").val(StartDate);
		$("#EndDate").val(EndDate);
	}
});

Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】,JS,layui,前端,javascript

2.2 第二种实现

//时间选择
layui.laydate.render({
    elem: '#Date_select',
    type: 'date',
    range: true,
    shortcuts: [
    	{
			text: '前三天',
			value: function () { return dateRange("前三天"); }()
		},
        {
            text: '昨天',
            value: function () { return dateRange("昨天"); }()
        },
        {
            text: '今天',
            value: function () { return dateRange("今天"); }()
        },
        {
            text: '上周',
            value: function () { return dateRange("上周"); }()
        },
        {
            text: '本周',
            value: function () { return dateRange("本周"); }()
        },
        {
            text: "上月",
            value: function () { return dateRange("上月"); }()
        },
        {
            text: "本月",
            value: function () { return dateRange("本月"); }()
        },
        {
            text: "上季度",
            value: function () { return dateRange("上季度"); }()
        },
        {
            text: "本季度",
            value: function () { return dateRange("本季度"); }()
        },
        {
            text: "上年",
            value: function () { return dateRange("上年"); }()
        },
        {
            text: "本年",
            value: function () { return dateRange("本年"); }()
        },
    ],
    done: function (value, date, endDate) {
        var StartDate = null;
        var EndDate = null;
        if (!!value) {
            StartDate = `${date.year}-${date.month}-${date.date} 00:00:00`;
            EndDate = `${endDate.year}-${endDate.month}-${endDate.date} 23:59:59`;
        }
        $("#StartDate").val(StartDate);
        $("#EndDate").val(EndDate);
    }
});
//日期范围选择快捷键获取数据
function dateRange(val) {
    let value = [];
    let startTime, endTime;
    let now = new Date(); //当前日期 
    let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天,周日为0,但实际上平时都是以周一为一周的开始,所以周3因该为2
    let nowDay = now.getDate(); //当前日 
    let nowMonth = now.getMonth(); //当前月 
    let nowYear = now.getFullYear(); //当前年 
    let jd = Math.ceil((nowMonth + 1) / 3)
    switch (val) {
        case "前三天":
			startTime = new Date(nowYear, nowMonth, nowDay - 3);
			endTime = new Date(nowYear, nowMonth, nowDay);
			break;
        case "昨天":
            startTime = new Date(nowYear, nowMonth, nowDay - 1);
            endTime = new Date(nowYear, nowMonth, nowDay);
            break;
        case "今天":
            startTime = new Date(nowYear, nowMonth, nowDay);
            endTime = new Date(nowYear, nowMonth, nowDay);
            break;
        case "明天":
            startTime = new Date(nowYear, nowMonth, nowDay + 1);
            endTime = new Date(nowYear, nowMonth, nowDay + 1);
            break;
        case "上周":
            debugger
            startTime = new Date(nowYear, nowMonth, (nowDay - nowDayOfWeek) - 7);
            endTime = new Date(nowYear, nowMonth, (nowDay + 6 - nowDayOfWeek) - 7);
            break;
        case "本周":
            startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
            endTime = new Date(nowYear, nowMonth, nowDay + 6 - nowDayOfWeek);
            break;
        case "上月":
            startTime = new Date(nowYear, nowMonth - 1, 1);
            endTime = new Date(nowYear, nowMonth, 0);
            break;
        case "本月":
            startTime = new Date(nowYear, nowMonth, 1);
            endTime = new Date(nowYear, nowMonth + 1, 0);
            break;
        case "上季度":
            startTime = new Date(nowYear, ((jd - 1) - 1) * 3, 1);
            endTime = new Date(nowYear, (jd - 1) * 3, 0);
            break
        case "本季度":
            startTime = new Date(nowYear, (jd - 1) * 3, 1);
            endTime = new Date(nowYear, jd * 3, 0);
            break
        case "上年":
            startTime = new Date(nowYear - 1, 0, 1);
            endTime = new Date(nowYear - 1, 11, 31);
            break
        case "本年":
            startTime = new Date(nowYear, 0, 1);
            endTime = new Date(nowYear, 11, 31);
            break
    }
    value.push(startTime);
    value.push(endTime);
    return value;
};

Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】,JS,layui,前端,javascript文章来源地址https://www.toymoban.com/news/detail-516531.html

到了这里,关于Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包