- Published on
6 Incredibly Easy Ways To Build A Updated Date Range Picker with Tailwind and Alpine JS With Tailwind CSS Better While Spending Less

- What is Tailwind CSS?
- The description of Updated Date Range Picker with Tailwind and Alpine JS ui component
- Why use Tailwind CSS to make a Updated Date Range Picker with Tailwind and Alpine JS ui component?
- The preview of Updated Date Range Picker with Tailwind and Alpine JS ui component
- The source code of Updated Date Range Picker with Tailwind and Alpine JS ui component
- How to make a Updated Date Range Picker with Tailwind and Alpine JS with Tailwind CSS?
- Install tailwind css of verion 2.2.19
- All the unility class needed to make a Updated Date Range Picker with Tailwind and Alpine JS component
- 53 steps to make a Updated Date Range Picker with Tailwind and Alpine JS component with Tailwind CSS
- Conclusion
What is Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.
The description of Updated Date Range Picker with Tailwind and Alpine JS ui component
This date range picker is based on my previous component (www.tailwindcsscomponent.com/date-range-picker-with-tailwind-and-alpine-js) but this one allows both ends of the range to be selected and also allows the range to be flipped.
Why use Tailwind CSS to make a Updated Date Range Picker with Tailwind and Alpine JS ui component?
- It can make the building process of Updated Date Range Picker with Tailwind and Alpine JS ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in Updated Date Range Picker with Tailwind and Alpine JS component file.
The preview of Updated Date Range Picker with Tailwind and Alpine JS ui component
Free download of the Updated Date Range Picker with Tailwind and Alpine JS's source code
The source code of Updated Date Range Picker with Tailwind and Alpine JS ui component
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/[email protected]^2/dist/tailwind.min.css">
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<style>
[x-cloak] {
display: none;
}
</style>
</head>
<body class="h-screen overflow-hidden flex justify-center">
<div class="h-screen w-screen flex justify-center bg-blue-100 ">
<div class="antialiased sans-serif">
<div x-data="app" x-cloak>
<div class="container mx-auto px-4 py-2 md:py-10">
<div>
<span class="font-bold my-1 text-gray-700 block">Results (would normally be hidden)</span>
<input type="text" name="date_from" x-model="dateFromYmd">
<input type="text" name="date_to" x-model="dateToYmd">
<label for="datepicker" class="font-bold mt-3 mb-1 text-gray-700 block">Select Date Range</label>
<div class="relative" @keydown.escape="closeDatepicker()" @click.outside="closeDatepicker()">
<div class="inline-flex items-center border rounded-md mt-3 bg-gray-200">
<input type="text" @click="endToShow = 'from'; init(); showDatepicker = true" x-model="outputDateFromValue" :class="{'font-semibold': endToShow == 'from' }" class="focus:outline-none border-0 p-2 w-40 rounded-l-md border-r border-gray-300"/>
<div class="inline-block px-2 h-full">to</div>
<input type="text" @click="endToShow = 'to'; init(); showDatepicker = true" x-model="outputDateToValue" :class="{'font-semibold': endToShow == 'to' }" class="focus:outline-none border-0 p-2 w-40 rounded-r-md border-l border-gray-300"/>
</div>
<div
class="bg-white mt-2 rounded-lg shadow p-4 absolute"
style="width: 17rem"
x-show="showDatepicker"
x-transition
>
<div class="flex flex-col items-center">
<div class="w-full flex justify-between items-center mb-2">
<div>
<span x-text="MONTH_NAMES[month]" class="text-lg font-bold text-gray-800"></span>
<span x-text="year" class="ml-1 text-lg text-gray-600 font-normal"></span>
</div>
<div>
<button
type="button"
class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full"
@click="if (month == 0) {year--; month=11;} else {month--;} getNoOfDays()">
<svg class="h-6 w-6 text-gray-500 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
</svg>
</button>
<button
type="button"
class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full"
@click="if (month == 11) {year++; month=0;} else {month++;}; getNoOfDays()">
<svg class="h-6 w-6 text-gray-500 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
</svg>
</button>
</div>
</div>
<div class="w-full flex flex-wrap mb-3 -mx-1">
<template x-for="(day, index) in DAYS" :key="index">
<div style="width: 14.26%" class="px-1">
<div
x-text="day"
class="text-gray-800 font-medium text-center text-xs"
></div>
</div>
</template>
</div>
<div class="flex flex-wrap -mx-1">
<template x-for="blankday in blankdays">
<div
style="width: 14.28%"
class="text-center border p-1 border-transparent text-sm"
></div>
</template>
<template x-for="(date, dateIndex) in no_of_days" :key="dateIndex">
<div style="width: 14.28%">
<div
@click="getDateValue(date, false)"
@mouseover="getDateValue(date, true)"
x-text="date"
class="p-1 cursor-pointer text-center text-sm leading-none leading-loose transition ease-in-out duration-100"
:class="{'font-bold': isToday(date) == true, 'bg-blue-800 text-white rounded-l-full': isDateFrom(date) == true, 'bg-blue-800 text-white rounded-r-full': isDateTo(date) == true, 'bg-blue-200': isInRange(date) == true }"
></div>
</div>
</template>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
document.addEventListener('alpine:init', () => {
Alpine.data('app', () => ({
showDatepicker: false,
dateFromYmd: '',
dateToYmd: '',
outputDateFromValue: '',
outputDateToValue: '',
dateFromValue: '',
dateToValue: '',
currentDate: null,
dateFrom: null,
dateTo: null,
endToShow: '',
selecting: false,
month: '',
year: '',
no_of_days: [],
blankdays: [],
convertFromYmd(dateYmd) {
const year = Number(dateYmd.substr(0, 4));
const month = Number(dateYmd.substr(5, 2)) - 1;
const date = Number(dateYmd.substr(8, 2));
return new Date(year, month, date);
},
convertToYmd(dateObject) {
const year = dateObject.getFullYear();
const month = dateObject.getMonth() + 1;
const date = dateObject.getDate();
return year + "-" + ('0' + month).slice(-2) + "-" + ('0' + date).slice(-2);
},
init() {
this.selecting = ( this.endToShow === 'to' && this.dateTo ) || ( this.endToShow === 'from' && this.dateFrom);
if ( ! this.dateFrom ) {
if ( this.dateFromYmd ) {
this.dateFrom = this.convertFromYmd( this.dateFromYmd );
}
}
if ( ! this.dateTo ) {
if ( this.dateToYmd ) {
this.dateTo = this.convertFromYmd( this.dateToYmd );
}
}
if ( ! this.dateFrom ) {
this.dateFrom = this.dateTo;
}
if ( ! this.dateTo ) {
this.dateTo = this.dateFrom;
}
if ( this.endToShow === 'from' && this.dateFrom ) {
this.currentDate = this.dateFrom;
} else if ( this.endToShow === 'to' && this.dateTo ) {
this.currentDate = this.dateTo;
} else {
this.currentDate = new Date();
}
currentMonth = this.currentDate.getMonth();
currentYear = this.currentDate.getFullYear();
if ( this.month !== currentMonth || this.year !== currentYear ) {
this.month = currentMonth;
this.year = currentYear;
this.getNoOfDays();
}
this.setDateValues();
},
isToday(date) {
const today = new Date();
const d = new Date(this.year, this.month, date);
return today.toDateString() === d.toDateString();
},
isDateFrom(date) {
const d = new Date(this.year, this.month, date);
if ( !this.dateFrom ) {
return false;
}
return d.getTime() === this.dateFrom.getTime();
},
isDateTo(date) {
const d = new Date(this.year, this.month, date);
if ( !this.dateTo ) {
return false;
}
return d.getTime() === this.dateTo.getTime();
},
isInRange(date) {
const d = new Date(this.year, this.month, date);
return d > this.dateFrom && d < this.dateTo;
},
outputDateValues() {
if (this.dateFrom) {
this.outputDateFromValue = this.dateFrom.toDateString();
this.dateFromYmd = this.convertToYmd(this.dateFrom);
}
if (this.dateTo) {
this.outputDateToValue = this.dateTo.toDateString();
this.dateToYmd = this.convertToYmd(this.dateTo);
}
},
setDateValues() {
if (this.dateFrom) {
this.dateFromValue = this.dateFrom.toDateString();
}
if (this.dateTo) {
this.dateToValue = this.dateTo.toDateString();
}
},
getDateValue(date, temp) {
// if we are in mouse over mode but have not started selecting a range, there is nothing more to do.
if (temp && !this.selecting) {
return;
}
let selectedDate = new Date(this.year, this.month, date);
if ( this.endToShow === 'from' ) {
this.dateFrom = selectedDate;
if ( ! this.dateTo ) {
this.dateTo = selectedDate;
} else if ( selectedDate > this.dateTo ) {
this.endToShow = 'to';
this.dateFrom = this.dateTo;
this.dateTo = selectedDate;
}
} else if ( this.endToShow === 'to' ) {
this.dateTo = selectedDate;
if ( ! this.dateFrom ) {
this.dateFrom = selectedDate;
} else if ( selectedDate < this.dateFrom ) {
this.endToShow = 'from';
this.dateTo = this.dateFrom;
this.dateFrom = selectedDate;
}
}
this.setDateValues();
if (!temp) {
if (this.selecting) {
this.outputDateValues();
this.closeDatepicker();
}
this.selecting = !this.selecting;
}
},
getNoOfDays() {
let daysInMonth = new Date(this.year, this.month + 1, 0).getDate();
// find where to start calendar day of week
let dayOfWeek = new Date(this.year, this.month).getDay();
let blankdaysArray = [];
for ( var i=1; i <= dayOfWeek; i++) {
blankdaysArray.push(i);
}
let daysArray = [];
for ( var i=1; i <= daysInMonth; i++) {
daysArray.push(i);
}
this.blankdays = blankdaysArray;
this.no_of_days = daysArray;
},
closeDatepicker() {
this.endToShow = '';
this.showDatepicker = false;
}
}))
})
</script>
</div>
</div>
</body>
</html>
How to make a Updated Date Range Picker with Tailwind and Alpine JS with Tailwind CSS?
Install tailwind css of verion 2.2.19
Use the script
html tag to import the script of Tailwind CSS of the version 2.2.19
<script src="https://cdn.tailwindcss.com"></script>
All the unility class needed to make a Updated Date Range Picker with Tailwind and Alpine JS component
h-screen
overflow-hidden
flex
w-screen
bg-blue-100
mx-auto
px-4
py-2
md:py-10
my-1
text-gray-700
block
mt-3
mb-1
relative
inline-flex
bg-gray-200
border-0
p-2
w-40
border-r
border-gray-300
inline-block
px-2
h-full
border-l
bg-white
mt-2
p-4
absolute
flex-col
w-full
mb-2
text-lg
text-gray-800
ml-1
text-gray-600
hover:bg-gray-200
p-1
h-6
w-6
text-gray-500
flex-wrap
mb-3
-mx-1
px-1
text-center
text-xs
border-transparent
text-sm
bg-blue-800
text-white
bg-blue-200
53 steps to make a Updated Date Range Picker with Tailwind and Alpine JS component with Tailwind CSS
Use
h-screen
to make an element span the entire height of the viewport.Use
overflow-hidden
to clip any content within an element that overflows the bounds of that element.Use
flex
to create a block-level flex container.Use
w-screen
to make an element span the entire width of the viewport.Control the background color of an element to blue-100 using the
bg-blue-100
utilities.Control the horizontal margin of an element to auto using the
mx-auto
utilities.Control the horizontal padding of an element to 1rem using the
px-4
utilities.Control the vertical padding of an element to 0.5rem using the
py-2
utilities.Control the vertical padding of an element to 2.5rem at only medium screen sizes using the
md:py-10
utilities.Control the vertical margin of an element to 0.25rem using the
my-1
utilities.Control the text color of an element to gray-700 using the
text-gray-700
utilities.Use
inline
utilities to put the element on its own line and fill its parent.Control the margin on top side of an element to 0.75rem using the
mt-3
utilities.Control the margin on bottom side of an element to 0.25rem using the
mb-1
utilities.Use
relative
to position an element according to the normal flow of the document.Use
inline-flex
to create an inline flex container that flows with text.Control the background color of an element to gray-200 using the
bg-gray-200
utilities.Control the border color of an element to 0rem using the
border-0
utilities.Control the padding on all sides of an element to 0.5rem using the
p-2
utilities.Use
w-40
to set an element to a fixed width(10rem).Control the border color of an element to r using the
border-r
utilities.Control the border color of an element to gray-300 using the
border-gray-300
utilities.Use
inline-block
utilities to wrap the element to prevent the text inside from extending beyond its parent.Control the horizontal padding of an element to 0.5rem using the
px-2
utilities.Use
h-full
to set an element’s height to 100% of its parent, as long as the parent has a defined height.Control the border color of an element to l using the
border-l
utilities.Control the background color of an element to white using the
bg-white
utilities.Control the margin on top side of an element to 0.5rem using the
mt-2
utilities.Control the padding on all sides of an element to 1rem using the
p-4
utilities.Use
absolute
to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.Use
flex
to create a block-level flex container.Use
w-full
to set an element to a 100% based width.Control the margin on bottom side of an element to 0.5rem using the
mb-2
utilities.Control the text color of an element to lg using the
text-lg
utilities.Control the text color of an element to gray-800 using the
text-gray-800
utilities.Control the margin on left side of an element to 0.25rem using the
ml-1
utilities.Control the text color of an element to gray-600 using the
text-gray-600
utilities.Control the background color of an element to gray-200 using the
hover:bg-gray-200
utilities on hover.Control the padding on all sides of an element to 0.25rem using the
p-1
utilities.Use
h-6
to set an element to a fixed height(1.5rem).Use
w-6
to set an element to a fixed width(1.5rem).Control the text color of an element to gray-500 using the
text-gray-500
utilities.Use
flex
to create a block-level flex container.Control the margin on bottom side of an element to 0.75rem using the
mb-3
utilities.Control the horizontal margin of an element to -0.25rem using the
-mx-1
utilities.Control the horizontal padding of an element to 0.25rem using the
px-1
utilities.Control the text color of an element to center using the
text-center
utilities.Control the text color of an element to xs using the
text-xs
utilities.Control the border color of an element to transparent using the
border-transparent
utilities.Control the text color of an element to sm using the
text-sm
utilities.Control the background color of an element to blue-800 using the
bg-blue-800
utilities.Control the text color of an element to white using the
text-white
utilities.Control the background color of an element to blue-200 using the
bg-blue-200
utilities.
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to make a Updated Date Range Picker with Tailwind and Alpine JS components, learn and follow along to implement your own components.