Date Range Picker
A month calendar where two clicks pick a start and an end, with a hover preview and arrow-key navigation.
AI prompt
Clean UI design of a single-month calendar for choosing a stay: a white rounded card with the month name and small previous and next arrows, a seven-column grid of day numbers, two selected days with solid dark circles and a light grey band joining the days between them with rounded ends, a summary line below reading 5 nights, 9 to 14 March, thin border, flat modern style. Variants: only the start selected, and no selection. Negative: heavy shadows, gradients, text errors, clutter. Reduced motion: static design.About this pattern
Definition
A range picker turns two choices into one gesture on a calendar: the first click sets the start, moving the pointer previews the range, the second click sets the end. The selected days are joined by a continuous band so the span reads at a glance.
Purpose & UX principles
- The first click starts a new range; the second completes it; if the second date is earlier, swap them.
- Preview the range on hover; shade days between start and end with a band and round the ends.
- Show the result in words: number of nights and the dates.
Interaction states
- A month grid, arrows to change month, a summary line and a Clear button.
Motion specification
- The band follows the hover instantly.
- Reduced motion: nothing to change.
Responsive behaviour
- Make it keyboard operable: arrow keys move by day and week, Enter selects.
- Disable past dates clearly and do not rely on colour alone.
Code
<div class="bx"><div class="cal"><div class="hd"><button type="button" class="ar" id="pm" aria-label="Previous month">‹</button><b id="mt"></b><button type="button" class="ar" id="nm" aria-label="Next month">›</button></div><div class="wk" aria-hidden="true"><span>Mo</span><span>Tu</span><span>We</span><span>Th</span><span>Fr</span><span>Sa</span><span>Su</span></div><div class="dy" id="dy" role="grid" aria-label="Choose dates"></div></div><div class="ft"><span class="out" id="out" role="status">Pick a start date.</span><button class="mini" id="cl" type="button">Clear</button></div></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.cal{border:1px solid var(--line);border-radius:12px;background:var(--surface);padding:8px 10px}.hd{display:flex;justify-content:space-between;align-items:center;font-size:13px;margin-bottom:4px}.ar{width:26px;height:26px;border-radius:50%;font-size:18px;line-height:1}.ar:hover{background:var(--pv)}
.wk,.dy{display:grid;grid-template-columns:repeat(7,1fr)}.wk span{font-size:10px;color:var(--muted);text-align:center;padding:2px 0}.dy button{height:26px;font-size:12px;position:relative;border-radius:0}.dy button.in{background:#ececea}.dy button.s,.dy button.e{background:#171717;color:#fff}.dy button.s{border-radius:99px 0 0 99px}.dy button.e{border-radius:0 99px 99px 0}.dy button.s.e{border-radius:99px}.dy button.pv:not(.s):not(.e){background:#f3f3f1}.dy button:disabled{color:#bbb;cursor:default}.dy button:hover:not(:disabled):not(.s):not(.e){background:#e3e3e0;border-radius:99px}
.ft{display:flex;justify-content:space-between;align-items:center;margin-top:8px}.out{font-size:12px;color:var(--muted)}
.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const dy=document.getElementById('dy'),out=document.getElementById('out');let y=2027,m=2,s=null,e=null,hv=null,foc=null;const MN=['January','February','March','April','May','June','July','August','September','October','November','December'];
const k=d=>new Date(y,m,d).getTime(),DAY=864e5,today=new Date(2027,2,3).getTime();
function paint(){const lo=s&&(e||hv)?Math.min(s,e||hv):s,hi=s&&(e||hv)?Math.max(s,e||hv):s;
dy.querySelectorAll('button').forEach(b=>{const t=k(+b.dataset.d),c=[];if(s&&t===lo)c.push('s');if(s&&t===hi)c.push('e');if(s&&t>lo&&t<hi)c.push('in');if(s&&!e&&hv&&t>=lo&&t<=hi)c.push('pv');b.className=c.join(' ');if(s&&(t===lo||t===hi))b.setAttribute('aria-selected','true');else b.removeAttribute('aria-selected');});
if(!s)out.textContent='Pick a start date.';else if(!e)out.textContent='Now pick an end date.';else{const nights=Math.round((Math.max(s,e)-Math.min(s,e))/DAY);out.textContent=nights+' night'+(nights>1?'s':'')+': '+new Date(Math.min(s,e)).getDate()+' to '+new Date(Math.max(s,e)).getDate()+' '+MN[new Date(Math.max(s,e)).getMonth()];}}
function draw(){document.getElementById('mt').textContent=MN[m]+' '+y;const first=(new Date(y,m,1).getDay()+6)%7,n=new Date(y,m+1,0).getDate();let h='';for(let i=0;i<first;i++)h+='<span></span>';
for(let d=1;d<=n;d++){h+='<button type="button" role="gridcell" data-d="'+d+'" tabindex="-1"'+(k(d)<today?' disabled':'')+' aria-label="'+d+' '+MN[m]+' '+y+'">'+d+'</button>';}
dy.innerHTML=h;const f=dy.querySelector('button:not(:disabled)');if(f){f.tabIndex=0;foc=+f.dataset.d;}paint();}
function pick(d){const t=k(d);if(!s||e){s=t;e=null;}else{e=t;if(e===s)e=null;}paint();}
dy.addEventListener('click',ev=>{const b=ev.target.closest('button');if(b&&!b.disabled)pick(+b.dataset.d);});
dy.addEventListener('mouseover',ev=>{const b=ev.target.closest('button');if(b&&s&&!e&&!b.disabled){hv=k(+b.dataset.d);paint();}});dy.addEventListener('mouseleave',()=>{hv=null;if(s&&!e)paint();});
dy.addEventListener('focusin',ev=>{const b=ev.target.closest('button');if(b){dy.querySelectorAll('button').forEach(x=>x.tabIndex=-1);b.tabIndex=0;foc=+b.dataset.d;}});
dy.addEventListener('keydown',ev=>{const dl={ArrowLeft:-1,ArrowRight:1,ArrowUp:-7,ArrowDown:7}[ev.key];if(dl){ev.preventDefault();const n=new Date(y,m+1,0).getDate();let nd=Math.max(1,Math.min(n,foc+dl));const t=dy.querySelector('[data-d="'+nd+'"]');if(t&&!t.disabled)t.focus();}});
document.getElementById('pm').addEventListener('click',()=>{m--;if(m<0){m=11;y--;}draw();});document.getElementById('nm').addEventListener('click',()=>{m++;if(m>11){m=0;y++;}draw();});document.getElementById('cl').addEventListener('click',()=>{s=e=hv=null;paint();});draw();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Date Range Picker — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.cal{border:1px solid var(--line);border-radius:12px;background:var(--surface);padding:8px 10px}.hd{display:flex;justify-content:space-between;align-items:center;font-size:13px;margin-bottom:4px}.ar{width:26px;height:26px;border-radius:50%;font-size:18px;line-height:1}.ar:hover{background:var(--pv)}
.wk,.dy{display:grid;grid-template-columns:repeat(7,1fr)}.wk span{font-size:10px;color:var(--muted);text-align:center;padding:2px 0}.dy button{height:26px;font-size:12px;position:relative;border-radius:0}.dy button.in{background:#ececea}.dy button.s,.dy button.e{background:#171717;color:#fff}.dy button.s{border-radius:99px 0 0 99px}.dy button.e{border-radius:0 99px 99px 0}.dy button.s.e{border-radius:99px}.dy button.pv:not(.s):not(.e){background:#f3f3f1}.dy button:disabled{color:#bbb;cursor:default}.dy button:hover:not(:disabled):not(.s):not(.e){background:#e3e3e0;border-radius:99px}
.ft{display:flex;justify-content:space-between;align-items:center;margin-top:8px}.out{font-size:12px;color:var(--muted)}
.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="cal"><div class="hd"><button type="button" class="ar" id="pm" aria-label="Previous month">‹</button><b id="mt"></b><button type="button" class="ar" id="nm" aria-label="Next month">›</button></div><div class="wk" aria-hidden="true"><span>Mo</span><span>Tu</span><span>We</span><span>Th</span><span>Fr</span><span>Sa</span><span>Su</span></div><div class="dy" id="dy" role="grid" aria-label="Choose dates"></div></div><div class="ft"><span class="out" id="out" role="status">Pick a start date.</span><button class="mini" id="cl" type="button">Clear</button></div></div>
</div>
<script>
const dy=document.getElementById('dy'),out=document.getElementById('out');let y=2027,m=2,s=null,e=null,hv=null,foc=null;const MN=['January','February','March','April','May','June','July','August','September','October','November','December'];
const k=d=>new Date(y,m,d).getTime(),DAY=864e5,today=new Date(2027,2,3).getTime();
function paint(){const lo=s&&(e||hv)?Math.min(s,e||hv):s,hi=s&&(e||hv)?Math.max(s,e||hv):s;
dy.querySelectorAll('button').forEach(b=>{const t=k(+b.dataset.d),c=[];if(s&&t===lo)c.push('s');if(s&&t===hi)c.push('e');if(s&&t>lo&&t<hi)c.push('in');if(s&&!e&&hv&&t>=lo&&t<=hi)c.push('pv');b.className=c.join(' ');if(s&&(t===lo||t===hi))b.setAttribute('aria-selected','true');else b.removeAttribute('aria-selected');});
if(!s)out.textContent='Pick a start date.';else if(!e)out.textContent='Now pick an end date.';else{const nights=Math.round((Math.max(s,e)-Math.min(s,e))/DAY);out.textContent=nights+' night'+(nights>1?'s':'')+': '+new Date(Math.min(s,e)).getDate()+' to '+new Date(Math.max(s,e)).getDate()+' '+MN[new Date(Math.max(s,e)).getMonth()];}}
function draw(){document.getElementById('mt').textContent=MN[m]+' '+y;const first=(new Date(y,m,1).getDay()+6)%7,n=new Date(y,m+1,0).getDate();let h='';for(let i=0;i<first;i++)h+='<span></span>';
for(let d=1;d<=n;d++){h+='<button type="button" role="gridcell" data-d="'+d+'" tabindex="-1"'+(k(d)<today?' disabled':'')+' aria-label="'+d+' '+MN[m]+' '+y+'">'+d+'</button>';}
dy.innerHTML=h;const f=dy.querySelector('button:not(:disabled)');if(f){f.tabIndex=0;foc=+f.dataset.d;}paint();}
function pick(d){const t=k(d);if(!s||e){s=t;e=null;}else{e=t;if(e===s)e=null;}paint();}
dy.addEventListener('click',ev=>{const b=ev.target.closest('button');if(b&&!b.disabled)pick(+b.dataset.d);});
dy.addEventListener('mouseover',ev=>{const b=ev.target.closest('button');if(b&&s&&!e&&!b.disabled){hv=k(+b.dataset.d);paint();}});dy.addEventListener('mouseleave',()=>{hv=null;if(s&&!e)paint();});
dy.addEventListener('focusin',ev=>{const b=ev.target.closest('button');if(b){dy.querySelectorAll('button').forEach(x=>x.tabIndex=-1);b.tabIndex=0;foc=+b.dataset.d;}});
dy.addEventListener('keydown',ev=>{const dl={ArrowLeft:-1,ArrowRight:1,ArrowUp:-7,ArrowDown:7}[ev.key];if(dl){ev.preventDefault();const n=new Date(y,m+1,0).getDate();let nd=Math.max(1,Math.min(n,foc+dl));const t=dy.querySelector('[data-d="'+nd+'"]');if(t&&!t.disabled)t.focus();}});
document.getElementById('pm').addEventListener('click',()=>{m--;if(m<0){m=11;y--;}draw();});document.getElementById('nm').addEventListener('click',()=>{m++;if(m>11){m=0;y++;}draw();});document.getElementById('cl').addEventListener('click',()=>{s=e=hv=null;paint();});draw();
</script>
</body></html>