Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>時間先後判定</h2>
<div class="d-flex gap-2">
  <!--   時間差判定範例 例 01 -->
  <ul class="d-flex flex-column gap-3 p-5 border border-2 w-50 list-unstyled">
    <li>例 01. 使用 flatpickr onChange 觸發時間判定,未使用 moment</li>
    <li>
      <label for="startDate" class="mb-16 mb-md-12">開始日期</label>
      <input type="datetime-local" id="startDate" name="startDate" placeholder="請點選日期" class="dateSelector">
    </li>
    <li>
      <label for="endDate" class="mb-16 mb-md-12">結束日期</label>
      <input type="datetime-local" id="endDate" name="endDate" placeholder="請點選日期" class="dateSelector">
      <p class="mt-3 text-danger d-none dateAlert">※ 結束日期須晚於開始日期</p>
    </li>
  </ul>
  <!--   時間差判定範例 例 02 -->
  <ul class="d-flex flex-column gap-3 p-5 border border-2 w-50 list-unstyled">
    <li>例 02. 僅使用 flatpickr UI,以傳統綁定 DOM 元素方法搭配 moment 作時間判定</li>
    <li>
      <label for="startDate2" class="mb-16 mb-md-12">開始日期</label>
      <input type="datetime-local" id="startDate2" name="startDate" placeholder="請點選日期" class="dateSelector2">
    </li>
    <li>
      <label for="endDate2" class="mb-16 mb-md-12">結束日期</label>
      <input type="datetime-local" id="endDate2" name="endDate" placeholder="請點選日期" class="dateSelector2">
      <p class="mt-3 text-danger d-none dateAlert2">※ 結束日期須晚於開始日期</p>
    </li>
  </ul>
</div>

<h2>flatpickr 傳值方法</h2>
<div class="d-flex flex-column gap-3 p-5 border border-2 w-50">
  <p>兩 input 間傳值</p>
  <div>
    <label for="Ainput" class="mb-16 mb-md-12">Ainput</label>
    <input type="datetime-local" id="Ainput" name="Ainput" placeholder="請點選日期">
  </div>
  <div>
    <label for="Binput" class="mb-16 mb-md-12">Binput</label>
    <input type="datetime-local" id="Binput" name="Binput" placeholder="請點選日期">
  </div>
  <button type="button" id="sendTime">傳值按我</button>
</div>
              
            
!

CSS

              
                body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

// 自定義 css
.flatpickr-day.selected,
.flatpickr-day.selected,
.flatpickr-day.selected.nextMonthDay,
.flatpickr-day.selected.prevMonthDay,
.flatpickr-day.selected:hover {
  background-color: white;
  border-color: #1e1e1e;
  color: #1e1e1e;
}

.flatpickr-day.today {
  background-color: #f2f2f2;
  border: none;
}

              
            
!

JS

              
                /* 時間差判定範例 例 01 ==================
==================================
==================================*/
const currentTime = new Date();

/* flat-pickr 設定*/
config = {
  enableTime: true, //可選時與分
  dateFormat: "Y-m-d H:i",
  time_24hr: true, //24 時制
  minuteIncrement: 15, //分鐘每次選擇間隔單位
  allowInput: true, //可輸入控制
  minDate: "today", //可選最小時間
  maxDate: currentTime.setMonth(currentTime.getMonth() + 1), //可選最大時間
  onChange: function (selectedDates, dateStr, instance) {
    checkDateTime(dateStr, instance);
    console.log(
      `例 01:你現在點選的是 ${instance.input.id},時間為 ${dateStr}`
    );
  }
};

flatpickr(".dateSelector", config);

/* 操作 */
const dateAlert = document.querySelector(".dateAlert");

let startDateValue = null;
let endDateValue = null;

function checkDateTime(dateStr, instance) {
  const elementId = instance.input.id;

  if (elementId === "startDate") {
    startDateValue = dateStr;
  } else {
    endDateValue = dateStr;
  }

  if (startDateValue > endDateValue) {
    dateAlert.classList.remove("d-none");
  } else {
    dateAlert.classList.add("d-none");
  }
}

/* 時間差判定範例 例 02 ==================
==================================
==================================*/
/* flat-pickr 設定*/
config = {
  enableTime: true, //可選時與分
  dateFormat: "Y/m/d H:i", //時間格式
  dateFormat: "Y-m-d H:i",
  time_24hr: true, //24 時制
  minuteIncrement: 15, //分鐘每次選擇間隔單位
  allowInput: true, //可輸入控制
  minDate: moment().format("YYYY/MM/DD"), //可選最小時間,搭配 moment.js 使用
  maxDate: moment().add(1, "months").format("YYYY/MM/DD") //可選最大時間,搭配 moment.js 使用
};

flatpickr(".dateSelector2", config);

/* 操作 */
const startDate2 = document.getElementById("startDate2");
const endDate2 = document.getElementById("endDate2");
const dateAlert2 = document.querySelector(".dateAlert2");

startDate2.addEventListener("change", checkDateTime2);
endDate2.addEventListener("change", checkDateTime2);

function checkDateTime2() {
  const startDateValue = moment(startDate2.value, "YYYY/MM/DD HH:mm");
  const endDateValue = moment(endDate2.value, "YYYY/MM/DD HH:mm");

  if (startDateValue.isValid() && endDateValue.isValid()) {
    //計算時間間距,以小時為單位
    const diffTime = endDateValue.diff(startDateValue, "hours");

    if (diffTime < 0) {
      dateAlert2.classList.remove("d-none");
    } else {
      dateAlert2.classList.add("d-none");
    }
  }
}

/* 傳值範例 ==================
==================================
==================================*/
/* flat-pickr 設定 */
const Ainput = flatpickr("#Ainput", {
  enableTime: true, //可選時與分
  dateFormat: "Y/m/d H:i", //時間格式
  time_24hr: true, //24 時制
  minuteIncrement: 15, //分鐘每次選擇間隔單位
  onChange: function (selectedDates, dateStr) {
    //selectedDates 是陣列,內容為選定日期時間 ISO 8601 格式
    //dateStr 是透過 flatpickr 格式化時間的 selectedDated[0]
    console.log(selectedDates);
    console.log(`你點擊的時間是 ${dateStr}`);
  }
});
const Binput = flatpickr("#Binput", {
  enableTime: true, //可選時與分
  dateFormat: "Y/m/d H:i", //時間格式
  time_24hr: true, //24 時制
  minuteIncrement: 15 //分鐘每次選擇間隔單位
});

/* 操作 */
const btnSendTime = document.getElementById("sendTime");
btnSendTime.addEventListener("click", function () {
  // 獲取 Ainput 的值,得到 ISO 8601 格式的時間字串
  const selectedDate = Ainput.selectedDates[0];

  if (selectedDate) {
    // 設定 Binput 的值為 Ainput 的值
    Binput.setDate(selectedDate);
  }
});

              
            
!
999px

Console