{"id":9701,"date":"2024-01-10T18:06:00","date_gmt":"2024-01-10T18:06:00","guid":{"rendered":"https:\/\/codehim.com\/?p=9701"},"modified":"2024-01-22T16:07:09","modified_gmt":"2024-01-22T11:07:09","slug":"custom-date-picker-javascript","status":"publish","type":"post","link":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/","title":{"rendered":"Custom Date Picker in JavaScript"},"content":{"rendered":"<p>This JavaScript code snippet helps you to create a custom date picker. It allows users to select a date from <a href=\"https:\/\/codehim.com\/bootstrap\/bootstrap-5-calendar-with-events\/\" target=\"_blank\" rel=\"noopener\">a calendar<\/a> that is displayed below the input field. The date picker is fully customizable, including the format of the date, the position of the calendar, and whether or not it is initially visible.<\/p>\n<p data-sourcepos=\"7:1-7:223\">The date picker creates a calendar object that represents the current month. The calendar object contains information about the days of the week, the number of days in the month, and whether or not the month is a leap year.<\/p>\n<p data-sourcepos=\"9:1-9:238\">When the user clicks on the date picker, a calendar popup is displayed. The calendar popup shows the days of the current month, as well as the previous and next months. The user can select a date from the calendar popup by clicking on it.<\/p>\n<h2>How to Create a Custom Date Picker In JavaScript<\/h2>\n<p>1. First, place the following HTML code in your project to initiate the date picker. Customize the <code>format<\/code> attribute to specify the desired date format.<\/p>\n<pre class=\"prettyprint linenums lang-html\">&lt;date-picker format=\"MMMM DD (DDD), YYYY\"&gt;&lt;\/date-picker&gt;<\/pre>\n<p>2. Basically, the date picker contains styles in the JS program. Anyhow, you can define the basic styles for the date picker container according to your needs.\u00a0(Optional)<\/p>\n<pre class=\"prettyprint linenums lang-css\">body {\r\n  width: 100vw;\r\n  height: 100vh;\r\n  display: flex;\r\n  justify-content: center;\r\n  align-items: center;\r\n}\r\n\r\n*, *::after, *::before {\r\n  box-sizing: border-box;\r\n}<\/pre>\n<p>3. Copy and paste the JavaScript code into your project. Feel free to modify the code to match your specific requirements. Initialize the date picker by selecting the element and setting initial attributes. Adjust the <code>date<\/code> attribute to pre-select a specific date and the <code>visible<\/code> attribute to control its initial visibility.<\/p>\n<pre class=\"prettyprint linenums lang-js\">function getWeekNumber(date) {\r\n  const firstDayOfTheYear = new Date(date.getFullYear(), 0, 1);\r\n\tconst pastDaysOfYear = (date.getTime() - firstDayOfTheYear.getTime()) \/ 86400000;\r\n\t\r\n\treturn Math.ceil((pastDaysOfYear + firstDayOfTheYear.getDay() + 1) \/ 7)\r\n}\r\n\r\nfunction isLeapYear(year) {\r\n  return year % 100 === 0 ? year % 400 === 0 : year % 4 === 0;\r\n}\r\n\r\nclass Day {\r\n  constructor(date = null, lang = 'default') {\r\n    date = date ?? new Date();\r\n    \r\n    this.Date = date;\r\n    this.date = date.getDate();\r\n    this.day = date.toLocaleString(lang, { weekday: 'long'});\r\n    this.dayNumber = date.getDay() + 1;\r\n    this.dayShort = date.toLocaleString(lang, { weekday: 'short'});\r\n    this.year = date.getFullYear();\r\n    this.yearShort = date.toLocaleString(lang, { year: '2-digit'});\r\n    this.month = date.toLocaleString(lang, { month: 'long'});\r\n    this.monthShort = date.toLocaleString(lang, { month: 'short'});\r\n    this.monthNumber = date.getMonth() + 1;\r\n    this.timestamp = date.getTime();\r\n    this.week = getWeekNumber(date);\r\n  }\r\n  \r\n  get isToday() {\r\n    return this.isEqualTo(new Date());\r\n  }\r\n  \r\n  isEqualTo(date) {\r\n    date = date instanceof Day ? date.Date : date;\r\n    \r\n    return date.getDate() === this.date &amp;&amp;\r\n      date.getMonth() === this.monthNumber - 1 &amp;&amp;\r\n      date.getFullYear() === this.year;\r\n  }\r\n  \r\n  format(formatStr) {\r\n    return formatStr\r\n      .replace(\/\\bYYYY\\b\/, this.year)\r\n      .replace(\/\\bYYY\\b\/, this.yearShort)\r\n      .replace(\/\\bWW\\b\/, this.week.toString().padStart(2, '0'))\r\n      .replace(\/\\bW\\b\/, this.week)\r\n      .replace(\/\\bDDDD\\b\/, this.day)\r\n      .replace(\/\\bDDD\\b\/, this.dayShort)\r\n      .replace(\/\\bDD\\b\/, this.date.toString().padStart(2, '0'))\r\n      .replace(\/\\bD\\b\/, this.date)\r\n      .replace(\/\\bMMMM\\b\/, this.month)\r\n      .replace(\/\\bMMM\\b\/, this.monthShort)\r\n      .replace(\/\\bMM\\b\/, this.monthNumber.toString().padStart(2, '0'))\r\n      .replace(\/\\bM\\b\/, this.monthNumber)\r\n  }\r\n}\r\n\r\nclass Month {\r\n  constructor(date = null, lang = 'default') {\r\n    const day = new Day(date, lang);\r\n    const monthsSize = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\r\n    this.lang = lang;\r\n    \r\n    this.name = day.month;\r\n    this.number = day.monthNumber;\r\n    this.year = day.year;\r\n    this.numberOfDays = monthsSize[this.number - 1];\r\n    \r\n    if(this.number === 2) {\r\n      this.numberOfDays += isLeapYear(day.year) ? 1 : 0;\r\n    }\r\n    \r\n    this[Symbol.iterator] = function* () {\r\n      let number = 1;\r\n      yield this.getDay(number);\r\n      while(number &lt; this.numberOfDays) {\r\n        ++number;\r\n        yield this.getDay(number);\r\n      }\r\n    }\r\n  }\r\n  \r\n  getDay(date) {\r\n    return new Day(new Date(this.year, this.number - 1, date), this.lang);\r\n  }\r\n}\r\n\r\nclass Calendar {\r\n  weekDays = Array.from({length: 7});\r\n  \r\n  constructor(year = null, monthNumber = null, lang = 'default') {\r\n    this.today = new Day(null, lang);\r\n    this.year = year ?? this.today.year;\r\n    this.month = new Month(new Date(this.year, (monthNumber || this.today.monthNumber) - 1), lang);\r\n    this.lang = lang;\r\n    \r\n    this[Symbol.iterator] = function* () {\r\n      let number = 1;\r\n      yield this.getMonth(number);\r\n      while(number &lt; 12) {\r\n        ++number;\r\n        yield this.getMonth(number);\r\n      }\r\n    }\r\n    \r\n    this.weekDays.forEach((_, i) =&gt; {\r\n      const day = this.month.getDay(i + 1);\r\n      if(!this.weekDays.includes(day.day)) {\r\n        this.weekDays[day.dayNumber - 1] = day.day\r\n      }\r\n    })\r\n  }\r\n  \r\n  get isLeapYear() {\r\n    return isLeapYear(this.year);\r\n  }\r\n  \r\n  getMonth(monthNumber) {\r\n    return new Month(new Date(this.year, monthNumber - 1), this.lang);\r\n  }\r\n  \r\n  getPreviousMonth() {\r\n    if(this.month.number === 1) {\r\n      return new Month(new Date(this.year - 1, 11), this.lang);\r\n    }\r\n    \r\n    return new Month(new Date(this.year, this.month.number - 2), this.lang);\r\n  }\r\n  \r\n  getNextMonth() {\r\n    if(this.month.number === 12) {\r\n      return new Month(new Date(this.year + 1, 0), this.lang);\r\n    }\r\n    \r\n    return new Month(new Date(this.year, this.month.number + 2), this.lang);\r\n  }\r\n  \r\n  goToDate(monthNumber, year) {\r\n    this.month = new Month(new Date(year, monthNumber - 1), this.lang);\r\n    this.year = year;\r\n  }\r\n  \r\n  goToNextYear() {\r\n    this.year += 1;\r\n    this.month = new Month(new Date(this.year, 0), this.lang);\r\n  }\r\n  \r\n  goToPreviousYear() {\r\n    this.year -= 1;\r\n    this.month = new Month(new Date(this.year, 11), this.lang);\r\n  }\r\n  \r\n  goToNextMonth() {\r\n    if(this.month.number === 12) {\r\n      return this.goToNextYear();\r\n    }\r\n    \r\n    this.month = new Month(new Date(this.year, (this.month.number + 1) - 1), this.lang);\r\n  }\r\n  \r\n  goToPreviousMonth() {\r\n    if(this.month.number === 1) {\r\n      return this.goToPreviousYear();\r\n    }\r\n    \r\n    this.month = new Month(new Date(this.year, (this.month.number - 1) - 1), this.lang);\r\n  }\r\n}\r\n\r\nclass DatePicker extends HTMLElement {\r\n  format = 'MMM DD, YYYY';\r\n  position = 'bottom';\r\n  visible = false;\r\n  date = null;\r\n  mounted = false;\r\n  \/\/ elements\r\n  toggleButton = null;\r\n  calendarDropDown = null;\r\n  calendarDateElement = null;\r\n  calendarDaysContainer = null;\r\n  selectedDayElement = null;\r\n  \r\n  constructor() {\r\n    super();\r\n    \r\n    const lang = window.navigator.language;\r\n    const date = new Date(this.date ?? (this.getAttribute(\"date\") || Date.now()));\r\n    \r\n    this.shadow = this.attachShadow({mode: \"open\"});\r\n    this.date = new Day(date, lang);\r\n    this.calendar = new Calendar(this.date.year, this.date.monthNumber, lang);\r\n    \r\n    this.format = this.getAttribute('format') || this.format;\r\n    this.position = DatePicker.position.includes(this.getAttribute('position'))\r\n      ? this.getAttribute('position')\r\n      : this.position;\r\n    this.visible = this.getAttribute('visible') === '' \r\n      || this.getAttribute('visible') === 'true'\r\n      || this.visible;\r\n    \r\n    this.render();\r\n  }\r\n  \r\n  connectedCallback() {\r\n    this.mounted = true;\r\n    \r\n    this.toggleButton = this.shadow.querySelector('.date-toggle');\r\n    this.calendarDropDown = this.shadow.querySelector('.calendar-dropdown');\r\n    const [prevBtn, calendarDateElement, nextButton] = this.calendarDropDown\r\n      .querySelector('.header').children;\r\n    this.calendarDateElement = calendarDateElement;\r\n    this.calendarDaysContainer = this.calendarDropDown.querySelector('.month-days');\r\n    \r\n    this.toggleButton.addEventListener('click', () =&gt; this.toggleCalendar());\r\n    prevBtn.addEventListener('click', () =&gt; this.prevMonth());\r\n    nextButton.addEventListener('click', () =&gt; this.nextMonth());\r\n    document.addEventListener('click', (e) =&gt; this.handleClickOut(e));\r\n    \r\n    this.renderCalendarDays();\r\n  }\r\n  \r\n  attributeChangedCallback(name, oldValue, newValue) {\r\n    if(!this.mounted) return;\r\n    \r\n    switch(name) {\r\n      case \"date\":\r\n        this.date = new Day(new Date(newValue));\r\n        this.calendar.goToDate(this.date.monthNumber, this.date.year);\r\n        this.renderCalendarDays();\r\n        this.updateToggleText();\r\n        break;\r\n      case \"format\":\r\n        this.format = newValue;\r\n        this.updateToggleText();\r\n        break;\r\n      case \"visible\":\r\n        this.visible = ['', 'true', 'false'].includes(newValue) \r\n          ? newValue === '' || newValue === 'true'\r\n          : this.visible;\r\n        this.toggleCalendar(this.visible);\r\n        break;\r\n      case \"position\":\r\n        this.position = DatePicker.position.includes(newValue)\r\n          ? newValue\r\n          : this.position;\r\n        this.calendarDropDown.className = \r\n          `calendar-dropdown ${this.visible ? 'visible' : ''} ${this.position}`;\r\n        break;\r\n    }\r\n  }\r\n  \r\n  toggleCalendar(visible = null) {\r\n    if(visible === null) {\r\n      this.calendarDropDown.classList.toggle('visible');\r\n    } else if(visible) {\r\n      this.calendarDropDown.classList.add('visible');\r\n    } else {\r\n      this.calendarDropDown.classList.remove('visible');\r\n    }\r\n    \r\n    this.visible = this.calendarDropDown.className.includes('visible');\r\n    \r\n    if(this.visible) {\r\n      this.calendarDateElement.focus();\r\n    } else {\r\n      this.toggleButton.focus();\r\n      \r\n      if(!this.isCurrentCalendarMonth()) {\r\n        this.calendar.goToDate(this.date.monthNumber, this.date.year);\r\n        this.renderCalendarDays();\r\n      }\r\n    }\r\n  }\r\n  \r\n  prevMonth() {\r\n    this.calendar.goToPreviousMonth();\r\n    this.renderCalendarDays();\r\n  }\r\n  \r\n  nextMonth() {\r\n    this.calendar.goToNextMonth();\r\n    this.renderCalendarDays();\r\n  }\r\n  \r\n  updateHeaderText() {\r\n    this.calendarDateElement.textContent = \r\n      `${this.calendar.month.name}, ${this.calendar.year}`;\r\n    const monthYear = `${this.calendar.month.name}, ${this.calendar.year}`\r\n    this.calendarDateElement\r\n      .setAttribute('aria-label', `current month ${monthYear}`);\r\n  }\r\n  \r\n  isSelectedDate(date) {\r\n    return date.date === this.date.date &amp;&amp;\r\n      date.monthNumber === this.date.monthNumber &amp;&amp;\r\n      date.year === this.date.year;\r\n  }\r\n  \r\n  isCurrentCalendarMonth() {\r\n    return this.calendar.month.number === this.date.monthNumber &amp;&amp;\r\n      this.calendar.year === this.date.year;\r\n  }\r\n  \r\n  selectDay(el, day) {\r\n    if(day.isEqualTo(this.date)) return;\r\n    \r\n    this.date = day;\r\n    \r\n    if(day.monthNumber !== this.calendar.month.number) {\r\n      this.prevMonth();\r\n    } else {\r\n      el.classList.add('selected');\r\n      this.selectedDayElement.classList.remove('selected');\r\n      this.selectedDayElement = el;\r\n    }\r\n    \r\n    this.toggleCalendar();\r\n    this.updateToggleText();\r\n  }\r\n  \r\n  handleClickOut(e) {\r\n    if(this.visible &amp;&amp; (this !== e.target)) {\r\n      this.toggleCalendar(false);\r\n    }\r\n  }\r\n  \r\n  getWeekDaysElementStrings() {\r\n    return this.calendar.weekDays\r\n      .map(weekDay =&gt; `&lt;span&gt;${weekDay.substring(0, 3)}&lt;\/span&gt;`)\r\n      .join('');\r\n  }\r\n  \r\n  getMonthDaysGrid() {\r\n    const firstDayOfTheMonth = this.calendar.month.getDay(1);\r\n    const prevMonth = this.calendar.getPreviousMonth();\r\n    const totalLastMonthFinalDays = firstDayOfTheMonth.dayNumber - 1;\r\n    const totalDays = this.calendar.month.numberOfDays + totalLastMonthFinalDays;\r\n    const monthList = Array.from({length: totalDays});\r\n    \r\n    for(let i = totalLastMonthFinalDays; i &lt; totalDays; i++) {\r\n      monthList[i] = this.calendar.month.getDay(i + 1 - totalLastMonthFinalDays)\r\n    }\r\n    \r\n    for(let i = 0; i &lt; totalLastMonthFinalDays; i++) {\r\n      const inverted = totalLastMonthFinalDays - (i + 1);\r\n      monthList[i] = prevMonth.getDay(prevMonth.numberOfDays - inverted);\r\n    }\r\n    \r\n    return monthList;\r\n  }\r\n  \r\n  updateToggleText() {\r\n    const date = this.date.format(this.format)\r\n    this.toggleButton.textContent = date;\r\n  }\r\n  \r\n  updateMonthDays() {\r\n    this.calendarDaysContainer.innerHTML = '';\r\n    \r\n    this.getMonthDaysGrid().forEach(day =&gt; {\r\n      const el = document.createElement('button');\r\n      el.className = 'month-day';\r\n      el.textContent = day.date;\r\n      el.addEventListener('click', (e) =&gt; this.selectDay(el, day));\r\n      el.setAttribute('aria-label', day.format(this.format));\r\n        \r\n      if(day.monthNumber === this.calendar.month.number) {\r\n        el.classList.add('current');\r\n      }\r\n\r\n      if(this.isSelectedDate(day)) {\r\n        el.classList.add('selected');\r\n        this.selectedDayElement = el;\r\n      }\r\n      \r\n      this.calendarDaysContainer.appendChild(el);\r\n    })\r\n  }\r\n  \r\n  renderCalendarDays() {\r\n    this.updateHeaderText();\r\n    this.updateMonthDays();\r\n    this.calendarDateElement.focus();\r\n  }\r\n  \r\n  static get observedAttributes() { \r\n    return ['date', 'format', 'visible', 'position']; \r\n  }\r\n    \r\n  static get position() {\r\n    return ['top', 'left', 'bottom', 'right'];\r\n  }\r\n  \r\n  get style() {\r\n    return `\r\n      :host {\r\n        position: relative;\r\n        font-family: sans-serif;\r\n      }\r\n      \r\n      .date-toggle {\r\n        padding: 8px 15px;\r\n        border: none;\r\n        -webkit-appearance: none;\r\n        -moz-appearance: none;\r\n        appearance: none;\r\n        background: #eee;\r\n        color: #333;\r\n        border-radius: 6px;\r\n        font-weight: bold;\r\n        cursor: pointer;\r\n        text-transform: capitalize;\r\n      }\r\n      \r\n      .calendar-dropdown {\r\n        display: none;\r\n        width: 300px;\r\n        height: 300px;\r\n        position: absolute;\r\n        top: 100%;\r\n        left: 50%;\r\n        transform: translate(-50%, 8px);\r\n        padding: 20px;\r\n        background: #fff;\r\n        border-radius: 5px;\r\n        box-shadow: 0 0 8px rgba(0,0,0,0.2);\r\n      }\r\n      \r\n      .calendar-dropdown.top {\r\n        top: auto;\r\n        bottom: 100%;\r\n        transform: translate(-50%, -8px);\r\n      }\r\n      \r\n      .calendar-dropdown.left {\r\n        top: 50%;\r\n        left: 0;\r\n        transform: translate(calc(-8px + -100%), -50%);\r\n      }\r\n      \r\n      .calendar-dropdown.right {\r\n        top: 50%;\r\n        left: 100%;\r\n        transform: translate(8px, -50%);\r\n      }\r\n      \r\n      .calendar-dropdown.visible {\r\n        display: block;\r\n      }\r\n      \r\n      .header {\r\n        display: flex;\r\n        justify-content: space-between;\r\n        align-items: center;\r\n        margin: 10px 0 30px;\r\n      }\r\n      \r\n      .header h4 {\r\n        margin: 0;\r\n        text-transform: capitalize;\r\n        font-size: 21px;\r\n        font-weight: bold;\r\n      }\r\n      \r\n      .header button {\r\n        padding: 0;\r\n        border: 8px solid transparent;\r\n        width: 0;\r\n        height: 0;\r\n        border-radius: 2px;\r\n        border-top-color: #222;\r\n        transform: rotate(90deg);\r\n        cursor: pointer;\r\n        background: none;\r\n        position: relative;\r\n      }\r\n      \r\n      .header button::after {\r\n        content: '';\r\n        display: block;\r\n        width: 25px;\r\n        height: 25px;\r\n        position: absolute;\r\n        left: 50%;\r\n        top: 50%;\r\n        transform: translate(-50%, -50%);\r\n      }\r\n      \r\n      .header button:last-of-type {\r\n        transform: rotate(-90deg);\r\n      }\r\n      \r\n      .week-days {\r\n        display: grid;\r\n        grid-template-columns: repeat(7, 1fr);\r\n        grid-gap: 5px;\r\n        margin-bottom: 10px;\r\n      }\r\n      \r\n      .week-days span {\r\n        display: flex;\r\n        justify-content: center;\r\n        align-items: center;\r\n        font-size: 10px;\r\n        text-transform: capitalize;\r\n      }\r\n      \r\n      .month-days {\r\n        display: grid;\r\n        grid-template-columns: repeat(7, 1fr);\r\n        grid-gap: 5px;\r\n      }\r\n      \r\n      .month-day {\r\n        padding: 8px 5px;\r\n        background: #c7c9d3;\r\n        color: #fff;\r\n        display: flex;\r\n        justify-content: center;\r\n        align-items: center;\r\n        border-radius: 2px;\r\n        cursor: pointer;\r\n        border: none;\r\n      }\r\n      \r\n      .month-day.current {\r\n        background: #444857;\r\n      }\r\n      \r\n      .month-day.selected {\r\n        background: #28a5a7;\r\n        color: #ffffff;\r\n      }\r\n      \r\n      .month-day:hover {\r\n        background: #34bd61;\r\n      }\r\n    `;\r\n  }\r\n  \r\n  render() {\r\n    const monthYear = `${this.calendar.month.name}, ${this.calendar.year}`;\r\n    const date = this.date.format(this.format)\r\n    this.shadow.innerHTML = `\r\n      &lt;style&gt;${this.style}&lt;\/style&gt;\r\n      &lt;button type=\"button\" class=\"date-toggle\"&gt;${date}&lt;\/button&gt;\r\n      &lt;div class=\"calendar-dropdown ${this.visible ? 'visible' : ''} ${this.position}\"&gt;\r\n        &lt;div class=\"header\"&gt;\r\n            &lt;button type=\"button\" class=\"prev-month\" aria-label=\"previous month\"&gt;&lt;\/button&gt;\r\n            &lt;h4 tabindex=\"0\" aria-label=\"current month ${monthYear}\"&gt;\r\n              ${monthYear}\r\n            &lt;\/h4&gt;\r\n            &lt;button type=\"button\" class=\"prev-month\" aria-label=\"next month\"&gt;&lt;\/button&gt;\r\n        &lt;\/div&gt;\r\n        &lt;div class=\"week-days\"&gt;${this.getWeekDaysElementStrings()}&lt;\/div&gt;\r\n        &lt;div class=\"month-days\"&gt;&lt;\/div&gt;\r\n      &lt;\/div&gt;\r\n    `\r\n  }\r\n}\r\n\r\ncustomElements.define(\"date-picker\", DatePicker);<\/pre>\n<p>Customize the date picker by adjusting attributes such as <code>format<\/code> and <code>position<\/code>. Experiment with different configurations to suit your project&#8217;s needs.<\/p>\n<pre class=\"prettyprint linenums lang-js\">\/\/ Example customization\r\ndatePicker.setAttribute('format', 'MMMM D, YYYY');\r\ndatePicker.setAttribute('position', 'top');<\/pre>\n<p>That&#8217;s all! hopefully, you have successfully created a custom date picker using JavaScript. If you have any questions or suggestions, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This JavaScript code snippet helps you to create a custom date picker. It allows users to select a date from&#8230;<\/p>\n","protected":false},"author":1,"featured_media":9703,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[58],"tags":[77],"class_list":["post-9701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-date-time","tag-datepicker"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Custom Date Picker in JavaScript &#8212; CodeHim<\/title>\n<meta name=\"description\" content=\"Here is a free code snippet to create a Custom Date Picker in JavaScript. You can view demo and download the source code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Date Picker in JavaScript &#8212; CodeHim\" \/>\n<meta property=\"og:description\" content=\"Here is a free code snippet to create a Custom Date Picker in JavaScript. You can view demo and download the source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeHim\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codehimofficial\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-10T18:06:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-22T11:07:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Asif Mughal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:site\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Asif Mughal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\"},\"author\":{\"name\":\"Asif Mughal\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\"},\"headline\":\"Custom Date Picker in JavaScript\",\"datePublished\":\"2024-01-10T18:06:00+00:00\",\"dateModified\":\"2024-01-22T11:07:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\"},\"wordCount\":297,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png\",\"keywords\":[\"Datepicker\"],\"articleSection\":[\"Date &amp; Time\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\",\"url\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\",\"name\":\"Custom Date Picker in JavaScript &#8212; CodeHim\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png\",\"datePublished\":\"2024-01-10T18:06:00+00:00\",\"dateModified\":\"2024-01-22T11:07:09+00:00\",\"description\":\"Here is a free code snippet to create a Custom Date Picker in JavaScript. You can view demo and download the source code.\",\"breadcrumb\":{\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage\",\"url\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png\",\"contentUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png\",\"width\":1280,\"height\":960,\"caption\":\"Custom Date Picker in JavaScript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codehim.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Date &amp; Time\",\"item\":\"https:\/\/codehim.com\/category\/date-time\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Custom Date Picker in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codehim.com\/#website\",\"url\":\"https:\/\/codehim.com\/\",\"name\":\"CodeHim\",\"description\":\"Web Design Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"alternateName\":\"Web Design Codes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codehim.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/codehim.com\/#organization\",\"name\":\"CodeHim - Web Design Code & Scripts\",\"url\":\"https:\/\/codehim.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"contentUrl\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"width\":280,\"height\":280,\"caption\":\"CodeHim - Web Design Code & Scripts\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/codehimofficial\",\"https:\/\/x.com\/CodeHimOfficial\",\"https:\/\/www.instagram.com\/codehim\/\",\"https:\/\/www.linkedin.com\/company\/codehim\",\"https:\/\/co.pinterest.com\/codehim\/\",\"https:\/\/www.youtube.com\/@codehim\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\",\"name\":\"Asif Mughal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"caption\":\"Asif Mughal\"},\"description\":\"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.\",\"sameAs\":[\"https:\/\/codehim.com\"],\"url\":\"https:\/\/codehim.com\/author\/asif-mughal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Custom Date Picker in JavaScript &#8212; CodeHim","description":"Here is a free code snippet to create a Custom Date Picker in JavaScript. You can view demo and download the source code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Custom Date Picker in JavaScript &#8212; CodeHim","og_description":"Here is a free code snippet to create a Custom Date Picker in JavaScript. You can view demo and download the source code.","og_url":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/","og_site_name":"CodeHim","article_publisher":"https:\/\/www.facebook.com\/codehimofficial","article_published_time":"2024-01-10T18:06:00+00:00","article_modified_time":"2024-01-22T11:07:09+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png","type":"image\/png"}],"author":"Asif Mughal","twitter_card":"summary_large_image","twitter_creator":"@CodeHimOfficial","twitter_site":"@CodeHimOfficial","twitter_misc":{"Written by":"Asif Mughal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#article","isPartOf":{"@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/"},"author":{"name":"Asif Mughal","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed"},"headline":"Custom Date Picker in JavaScript","datePublished":"2024-01-10T18:06:00+00:00","dateModified":"2024-01-22T11:07:09+00:00","mainEntityOfPage":{"@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/"},"wordCount":297,"commentCount":0,"publisher":{"@id":"https:\/\/codehim.com\/#organization"},"image":{"@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png","keywords":["Datepicker"],"articleSection":["Date &amp; Time"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/","url":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/","name":"Custom Date Picker in JavaScript &#8212; CodeHim","isPartOf":{"@id":"https:\/\/codehim.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage"},"image":{"@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png","datePublished":"2024-01-10T18:06:00+00:00","dateModified":"2024-01-22T11:07:09+00:00","description":"Here is a free code snippet to create a Custom Date Picker in JavaScript. You can view demo and download the source code.","breadcrumb":{"@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#primaryimage","url":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png","contentUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2023\/11\/Custom-Date-Picker-in-JavaScript.png","width":1280,"height":960,"caption":"Custom Date Picker in JavaScript"},{"@type":"BreadcrumbList","@id":"https:\/\/codehim.com\/date-time\/custom-date-picker-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codehim.com\/"},{"@type":"ListItem","position":2,"name":"Date &amp; Time","item":"https:\/\/codehim.com\/category\/date-time\/"},{"@type":"ListItem","position":3,"name":"Custom Date Picker in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/codehim.com\/#website","url":"https:\/\/codehim.com\/","name":"CodeHim","description":"Web Design Code Snippets","publisher":{"@id":"https:\/\/codehim.com\/#organization"},"alternateName":"Web Design Codes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codehim.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codehim.com\/#organization","name":"CodeHim - Web Design Code & Scripts","url":"https:\/\/codehim.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/","url":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","contentUrl":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","width":280,"height":280,"caption":"CodeHim - Web Design Code & Scripts"},"image":{"@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codehimofficial","https:\/\/x.com\/CodeHimOfficial","https:\/\/www.instagram.com\/codehim\/","https:\/\/www.linkedin.com\/company\/codehim","https:\/\/co.pinterest.com\/codehim\/","https:\/\/www.youtube.com\/@codehim"]},{"@type":"Person","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed","name":"Asif Mughal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","caption":"Asif Mughal"},"description":"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.","sameAs":["https:\/\/codehim.com"],"url":"https:\/\/codehim.com\/author\/asif-mughal\/"}]}},"views":3355,"_links":{"self":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/9701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/comments?post=9701"}],"version-history":[{"count":0,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/9701\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media\/9703"}],"wp:attachment":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media?parent=9701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/categories?post=9701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/tags?post=9701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}