# Airbnb · 放宽日期查找可订房源

搜索住宿时选好入住和退房日期，再选择日期可以前后浮动几天。例如设为前后三天，也能找到原计划日期附近可订的房源。

[官方来源](https://www.airbnb.com/help/article/252)

## 相关动效 · 日期范围选择器

# 日期范围选择器

先后选中两个日期，即可设定一段可跨月的日期范围。

## 试一试

先选起始日期，再选结束日期。试试跨月选择和清除日期。

## 交互细节

第一次选择锚定起始日期，第二次选择确认范围。移动指针或键盘焦点时预览区间。天数包含首尾两天，反向选取的日期会按先后排列。

## 各平台的操作方式

- iOS · 轻点开始和结束日期。要跨月选择，可用月份切换箭头。
- Android · 点按两个日期设定范围，再试试清除日期。
- PC · 用方向键移动，按回车或空格选定两个端点，用 Page Up 和 Page Down 切换月份。

## 适用场景

预约、旅行或报表筛选中的日期范围输入。

## 实现要求

> 预览两个日期之间的范围，支持跨月和反向选择，并准确计算含首尾的天数。

## 来源与致谢

[Adobe React Aria · RangeCalendar](<https://react-aria.adobe.com/RangeCalendar>)

## 独立 HTML 示例

将以下代码保存为 HTML 文件，用浏览器打开即可离线体验。

```html
<!doctype html>
<html lang="zh-Hans"><head><script>if(new URLSearchParams(location.search).get('reduce')==='1')document.documentElement.classList.add('motion-reduced');</script><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>工作室预约日历</title><style>
*{box-sizing:border-box}body{margin:0;min-height:100vh;display:grid;align-items:center;background:#dce5e9;color:#182e41;font:15px/1.4 system-ui,sans-serif}main{width:min(735px,100%);margin:auto;padding:clamp(10px,4%,34px)}.planner{display:grid;grid-template-columns:minmax(145px,.7fr) minmax(0,1.55fr);overflow:hidden;background:#fffdf7;border:1px solid #9baeb4;box-shadow:0 9px 28px #42595d20}.record{background:#24455c;color:#fff8e8;display:flex;flex-direction:column;padding:clamp(13px,3vw,28px);gap:12px}.record h1{font-size:clamp(20px,3vw,27px);margin:0;letter-spacing:-.06em;word-break:keep-all}.dates{margin:auto 0 0;display:grid;gap:4px}.dates small{color:#d5e4e9}.dates strong{font:400 clamp(31px,5vw,54px)/1.13 Georgia,serif;font-variant-numeric:tabular-nums}.record .clear{border:0;background:transparent;color:#fff8e8;border-bottom:1px solid #a9c1cd;align-self:flex-start;padding:3px 0;min-height:44px;font:inherit;font-size:13px;cursor:pointer}.cal{padding:clamp(12px,3vw,23px)}.month,.nav{display:flex;align-items:center;gap:6px}.month{justify-content:space-between}.month h2{margin:0;font:700 clamp(19px,3vw,26px)/1.25 system-ui;letter-spacing:-.045em}.nav button{border:1px solid #9aaab1;background:#faf9f1;color:#263e4c;min-width:40px;min-height:42px;font-size:19px;cursor:pointer}.nav button:disabled{opacity:.4;cursor:default}table{width:100%;table-layout:fixed;text-align:center;border-collapse:separate;border-spacing:0 4px;margin:6px 0 0}th{color:#536877;font-weight:500;font-size:12px}td{padding:0}td button{width:100%;height:39px;border:none;border-radius:0;background:#f7f7ef;color:#253b4a;font:600 15px/1.2 ui-monospace,monospace;cursor:pointer;transition:background-color .14s}td button.inside{background:#cadbd8;color:#173d41}td button.edge{background:#275450;color:white}td button.start{border-radius:20px 0 0 20px}td button.end{border-radius:0 20px 20px 0}td button.start.end{border-radius:20px}td button.draft.inside:not(.edge){background:#deebe6}.hint{margin:3px 0 0;min-height:2.9em;font-size:13px;color:#40596b}button:focus-visible{outline:3px solid #b45119;outline-offset:-3px}.record button:focus-visible{outline-color:#ffc884}.motion-reduced td button{transition:none}@media(prefers-reduced-motion:reduce){td button{transition:none}}@media(max-width:500px){.planner{grid-template-columns:1fr}.record{display:grid;grid-template-columns:1fr auto;gap:3px 14px;padding:11px 14px}.record h1{grid-column:1/-1;font-size:19px}.dates{display:flex;align-items:baseline;gap:7px}.dates strong{font-size:25px}.record .clear{padding:0}.cal{padding:13px 10px}.nav button{min-height:44px}.hint{min-height:0}.dates small{font-size:12px}td button{height:40px}}
</style></head><body><main><div class="planner"><header class="record"><h1>工作室预约日历</h1><p class="dates"><small>所选日期</small><strong id="dayCount">4 天</strong></p><button class="clear" type="button" id="clear">清除日期</button></header><section class="cal" aria-labelledby="month"><div class="month"><h2 id="month" aria-live="polite"></h2><div class="nav"><button type="button" id="prev" aria-label="上个月">‹</button><button type="button" id="next" aria-label="下个月">›</button></div></div><table aria-label="选择工作室预约日期"><thead><tr><th scope="col">日</th><th scope="col">一</th><th scope="col">二</th><th scope="col">三</th><th scope="col">四</th><th scope="col">五</th><th scope="col">六</th></tr></thead><tbody id="days"></tbody></table><p class="hint" role="status" id="hint"></p></section></div></main>
<script>
(()=>{
  const get=id=>document.getElementById(id),days=get('days'),day=86400000;
  const min=Date.UTC(1900,0,1),max=Date.UTC(2199,11,31),first=Date.UTC(2026,10,17);
  let start=first,end=first+3*day,point=first,preview=null,month=Date.UTC(2026,10,1);
  const y=d=>new Date(d).getUTCFullYear(),m=d=>new Date(d).getUTCMonth(),n=d=>new Date(d).getUTCDate();
  const short=d=>(m(d)+1)+'月'+n(d)+'日';
  const full=d=>y(d)+'年'+short(d);
  function bound(d){return Math.max(min,Math.min(max,d));}
  function shifted(d,months){const last=Date.UTC(y(d),m(d)+months+1,0);return bound(Date.UTC(y(last),m(last),Math.min(n(last),n(d))));}
  function paint(){
    const last=end??preview??start;
    const lo=start===null?null:Math.min(start,last), hi=start===null?null:Math.max(start,last);
    for(const btn of days.querySelectorAll('button')){
      const date=Number(btn.dataset.day),inRange=lo!==null&&date>=lo&&date<=hi;
      btn.classList.toggle('inside',inRange);btn.classList.toggle('edge',date===lo||date===hi);btn.classList.toggle('start',date===lo);btn.classList.toggle('end',date===hi);btn.classList.toggle('draft',end===null);
      btn.setAttribute('aria-pressed',String(start!==null&&(end===null?date===start:inRange)));
      btn.tabIndex=date===point?0:-1;
    }
    get('dayCount').textContent=lo===null?'—':((hi-lo)/day+1)+' 天';
    const message=start===null?'先选择开始日期，再选择结束日期。':end===null?short(start)+' 起 · 请选择结束日期。':full(start)+' — '+(y(start)===y(end)?short(end):full(end));
    if(get('hint').textContent!==message)get('hint').textContent=message;
  }
  function render(focus){
    days.replaceChildren();
    const total=n(Date.UTC(y(month),m(month)+1,0)),offset=new Date(month).getUTCDay(),cellCount=Math.ceil((offset+total)/7)*7;
    for(let index=0;index<cellCount;index++){
      let row=index%7===0?days.insertRow():days.lastElementChild;
      const cell=row.insertCell(),num=index-offset+1;
      if(num<1||num>total)continue;
      const d=month+(num-1)*day,b=document.createElement('button');b.type='button';b.dataset.day=String(d);b.textContent=num;b.setAttribute('aria-label',full(d));cell.append(b);
    }
    get('month').textContent=y(month)+'年'+(m(month)+1)+'月';
    get('prev').disabled=month===min;get('next').disabled=y(month)===2199&&m(month)===11;
    paint();if(focus)days.querySelector('[tabindex="0"]')?.focus();
  }
  function move(date,focus){
    point=bound(date);const next=Date.UTC(y(point),m(point),1);
    preview=end===null?point:null;
    if(next!==month){month=next;render(focus);}else{paint();if(focus)days.querySelector('[tabindex="0"]')?.focus();}
  }
  days.addEventListener('click',e=>{
    const b=e.target.closest('button');if(!b)return;
    point=Number(b.dataset.day);
    if(start===null||end!==null){start=point;end=null;preview=point;}
    else{end=Math.max(start,point);start=Math.min(start,point);preview=null;}
    paint();
  });
  days.addEventListener('pointerover',e=>{const b=e.target.closest('button');if(b&&start!==null&&end===null){preview=Number(b.dataset.day);paint();}});
  days.addEventListener('pointerleave',()=>{if(end===null){preview=point;paint();}});
  days.addEventListener('keydown',e=>{
    const b=e.target.closest('button');if(!b)return;
    const d=Number(b.dataset.day),weekday=new Date(d).getUTCDay();
    const values={ArrowLeft:d-day,ArrowRight:d+day,ArrowUp:d-7*day,ArrowDown:d+7*day,Home:d-weekday*day,End:d+(6-weekday)*day,PageUp:shifted(d,-1),PageDown:shifted(d,1)};
    if(!Object.hasOwn(values,e.key))return;e.preventDefault();move(values[e.key],true);
  });
  get('prev').addEventListener('click',()=>move(shifted(point,-1),false));get('next').addEventListener('click',()=>move(shifted(point,1),false));
  get('clear').addEventListener('click',()=>{start=end=preview=null;paint();days.querySelector('[tabindex="0"]').focus();});render(false);
})();
</script></body></html>
```
