You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
layui/docs/laydate/examples/shortcut.md

559 lines
13 KiB

2 years ago
<div class="layui-form">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">日期</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-date">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">年份</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-year">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">年月</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-month">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">时间</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-time">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">日期时间</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-datetime">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">日期时间全面板</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-datetime-fullPanel">
</div>
</div>
<hr>
<div class="layui-inline">
<label class="layui-form-label">日期范围</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-range-date" placeholder=" - ">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">年份范围</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-range-year" placeholder=" - ">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">年月范围</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-range-month" placeholder=" - ">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">时间范围</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="ID-laydate-shortcut-range-time" placeholder=" - ">
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">日期时间范围</label>
<div class="layui-input-block">
<input type="text" class="layui-input" id="ID-laydate-shortcut-range-datetime" placeholder=" - ">
</div>
</div>
</div>
<!-- import layui -->
2 years ago
<script>
layui.use(function(){
var laydate = layui.laydate;
var util = layui.util;
/*
* 快捷选项
*/
// 日期
laydate.render({
elem: "#ID-laydate-shortcut-date",
shortcuts: [
{
text: "昨天",
value: function(){
var now = new Date();
now.setDate(now.getDate() - 1);
return now;
}
},
{
text: "今天",
value: function(){
return Date.now();
}
2 years ago
},
{
text: "明天",
value: function(){
var now = new Date();
now.setDate(now.getDate() + 1);
return now;
}
2 years ago
},
{
text: "上个月",
value: function(){
var now = new Date();
var month = now.getMonth() - 1;
now.setMonth(month);
// 若上个月数不匹配,则表示天数溢出
if (now.getMonth() !== month) {
now.setDate(0); // 重置天数
}
2 years ago
return [now];
}
2 years ago
},
{
text: "下个月",
2 years ago
value: function(){
var now = new Date();
var month = now.getMonth() + 1;
now.setMonth(month);
// 若下个月数不匹配,则表示天数溢出
if (now.getMonth() !== month) {
now.setDate(0); // 重置天数
}
2 years ago
return [now];
}
2 years ago
},
{
text: "某一天",
value: "2016-10-14"
}
]
});
// 年份
laydate.render({
elem: "#ID-laydate-shortcut-year",
type: "year",
shortcuts: [
{
text: "去年",
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() - 1);
return now;
}
2 years ago
},
{
text: "明年",
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() + 1);
return now;
}
2 years ago
}
]
});
// 年月
laydate.render({
elem: "#ID-laydate-shortcut-month",
type: "month",
shortcuts: [
{
text: "上个月",
value: function(){
var now = new Date();
now.setMonth(now.getMonth() - 1, 1);
2 years ago
return now;
}
2 years ago
},
{
text: "下个月",
value: function(){
var now = new Date();
now.setMonth(now.getMonth() + 1, 1);
2 years ago
return now;
}
2 years ago
},
{
text: "去年本月",
2 years ago
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() - 1);
return now;
}
2 years ago
}
]
});
// 时间
laydate.render({
elem: "#ID-laydate-shortcut-time",
type: "time",
shortcuts: function(){ // 生成 30 分钟间隔的时间列表
var value = [];
var now = new Date();
now.setHours(0, 0, 0, 0);
for (var i = 0; i < 48; i++) {
var nowTemp = now.setMinutes(now.getMinutes() + (i ? 30 : 0));
var nowTimeStr = util.toDateString(nowTemp, "HH:mm:ss");
value.push({
text: nowTimeStr,
value: nowTimeStr
});
}
return value;
}()
});
// 日期时间
laydate.render({
elem: "#ID-laydate-shortcut-datetime",
type: "datetime",
shortcuts: [
{
text: "昨天",
value: function(){
var now = new Date();
now.setDate(now.getDate() - 1);
return now;
}
},
{
text: "今天",
value: function(){
return Date.now();
}
2 years ago
},
{
text: "明天",
value: function(){
var now = new Date();
now.setDate(now.getDate() + 1);
return now;
}
2 years ago
},
{
text: "上个月",
value: function(){
var now = new Date();
var month = now.getMonth() - 1;
now.setMonth(month);
// 若上个月数不匹配,则表示天数溢出
if (now.getMonth() !== month) {
now.setDate(0); // 重置天数
}
2 years ago
return [now];
}
2 years ago
},
{
text: "下个月",
2 years ago
value: function(){
var now = new Date();
var month = now.getMonth() + 1;
now.setMonth(month);
// 若上个月数不匹配,则表示天数溢出
if (now.getMonth() !== month) {
now.setDate(0); // 重置天数
}
2 years ago
return [now];
}
2 years ago
},
{
text: "某一天",
value: "2016-10-14 10:00:00"
2 years ago
}
]
});
// 日期时间全面板
laydate.render({
elem: "#ID-laydate-shortcut-datetime-fullPanel",
type: "datetime",
fullPanel: true,
shortcuts: [
{
text: "昨天",
value: function(){
var now = new Date();
now.setDate(now.getDate() - 1);
return now;
}
2 years ago
},
{ text: "今天", value: Date.now() },
{
text: "明天",
value: function(){
var now = new Date();
now.setDate(now.getDate() + 1);
return now;
}
2 years ago
},
{
text: "上个月",
value: function(){
var now = new Date();
var month = now.getMonth() - 1;
now.setMonth(month);
// 若上个月数不匹配,则表示天数溢出
if (now.getMonth() !== month) {
now.setDate(0); // 重置天数
}
2 years ago
return [now];
}
2 years ago
},
{
text: "下个月",
2 years ago
value: function(){
var now = new Date();
var month = now.getMonth() + 1;
now.setMonth(month);
// 若上个月数不匹配,则表示天数溢出
if (now.getMonth() !== month) {
now.setDate(0); // 重置天数
}
2 years ago
return [now];
}
2 years ago
},
{
text: "某一天",
value: "2016-10-14 09:30:00"
2 years ago
}
]
});
// 日期范围
laydate.render({
elem: "#ID-laydate-shortcut-range-date",
range: true,
shortcuts: [
{
text: "上个月",
value: function(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
return [
new Date(year, month - 1, 1),
new Date(year, month, 0)
];
}
2 years ago
},
{
text: "这个月",
value: function(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
return [
new Date(year, month, 1),
new Date(year, month + 1, 0)
];
}
2 years ago
},
{
text: "下个月",
value: function(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
return [
new Date(year, month + 1, 1),
new Date(year, month + 2, 0)
];
}
2 years ago
}
]
});
// 年份范围
laydate.render({
elem: "#ID-laydate-shortcut-range-year",
type: "year",
range: true,
shortcuts: [
{
text: "过去一年",
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() - 1);
return [now, new Date()];
}
2 years ago
},
{
text: "未来一年",
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() + 1);
return [new Date(), now];
}
2 years ago
},
{
text: "近三年",
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() - 3);
return [now, new Date()];
}
2 years ago
}
]
});
// 年月范围
laydate.render({
elem: "#ID-laydate-shortcut-range-month",
type: "month",
range: true,
shortcuts: [
{
text: "去年",
value: function(){
var now = new Date();
var year = now.getFullYear() - 1;
return [
new Date(year, 0),
new Date(year, 11)
];
}
2 years ago
},
{
text: "明年",
value: function(){
var now = new Date();
var year = now.getFullYear() + 1;
return [
new Date(year, 0),
new Date(year, 11)
];
}
2 years ago
},
{
text: "近三年",
value: function(){
var now = new Date();
now.setFullYear(now.getFullYear() - 3);
return [now, new Date()];
}
2 years ago
}
]
});
// 时间范围
laydate.render({
elem: "#ID-laydate-shortcut-range-time",
type: "time",
range: true,
shortcuts: [
{
text: '09:30 <p style="text-align: center;"></p> 11:30',
value: (function () {
var date1 = new Date();
date1.setHours(9, 0, 0, 0);
var date2 = new Date();
date2.setHours(11, 30, 0, 0);
return [date1, date2];
})
2 years ago
},
{
text: '13:00 <p style="text-align: center;"></p> 15:00',
value: (function () {
var date1 = new Date();
date1.setHours(13, 0, 0, 0);
var date2 = new Date();
date2.setHours(15, 0, 0, 0);
return [date1, date2];
})
2 years ago
}
]
});
// 日期时间范围
laydate.render({
elem: "#ID-laydate-shortcut-range-datetime",
type: "datetime",
range: true,
shortcuts: [
{
text: "上个月",
value: function(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
return [
new Date(year, month - 1, 1),
new Date(year, month, 0, 23, 59, 59)
];
}
2 years ago
},
{
text: "这个月",
value: function(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
return [
new Date(year, month, 1),
new Date(year, month + 1, 0, 23, 59, 59)
];
}
2 years ago
},
{
text: "下个月",
value: function(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
return [
new Date(year, month + 1, 1),
new Date(year, month + 2, 0, 23, 59, 59)
];
}
2 years ago
}
]
});
});
</script>