欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

el-date-picker日期時間選擇器的選擇時間限制到分鐘級別

 更新時間:2025年01月13日 10:11:05   作者:前端程序員_花姐夫Jun  
文章介紹了如何使用el-date-picker 組件來限制用戶選擇的時間,禁止選擇當前時間的日期及時分,同時允許選擇其他日期的全天時分,通過設置 `pickerOptions` 對象的屬性,可以實現(xiàn)對日期和時間的精確控制,感興趣的朋友跟隨小編一起看看吧

有個需求是限制選擇的時間,禁止選擇當前時間的日期及時分,如果日期選擇的不是今天,時間還是要能選擇全天的時分

一、代碼展示

<template>
  <el-date-picker
    v-model="date"
    type="datetime"
    format="YYYY-MM-DD HH:mm"
    time-format="HH:mm"
    v-bind="pickerOptions"
    placeholder="選擇日期時間">
  </el-date-picker>
</template>
<script lang="ts" setup>
import { ref, computed } from 'vue'
const date = ref('')
/**
 * 生成一個數(shù)組
 * @param start
 * @param end
 */
const makeRange = (start: number, end: number) => {
  const result: number[] = []
  for (let i = start; i <= end; i++) {
    result.push(i)
  }
  return result
}
/**
 * 限制今天之前的時間不能選擇(小時)
 */
const disabledHours = () => {
  let newVal = new Date(date.value)
  if (newVal && newVal.getFullYear() == new Date().getFullYear() &&
    newVal.getMonth() == new Date().getMonth() &&
    newVal.getDate() == new Date().getDate()
  ) {
    //如果為今天,則限制當前時間前的時間不能選擇。
    return makeRange(0, new Date().getHours() - 1)
  }
}
/**
 * 限制今天之前的時間不能選擇(分鐘)
 * @param hour
 */
const disabledMinutes = (hour: number) => {
  let newVal = new Date(date.value)
  if (newVal && newVal.getFullYear() == new Date().getYear() &&
    newVal.getMonth() == new Date().getMonth() &&
    newVal.getDate() == new Date().getDate()
  ) {
    //如果為今天,則限制當前時間前的時間不能選擇。
    return makeRange(0, new Date().getMinutes() - 1)
  }
}
/**
 * 限制今天之前的時間不能選擇的配置
 */
const pickerOptions = computed(() => {
  return {
    // 限制今天之前的日期不能選擇
    disabledDate(time: any) {
      return time.getTime() < Date.now() - 8.64e7
    },
    // 限制今天之前的小時不能選擇
    disabledHours,
    // 限制今天之前的分鐘不能選擇
    disabledMinutes
  }
})
</script>

二、代碼解釋 模板部分 (<template>)

- 使用 el-date-picker 組件:

- v-model="date": 將用戶選擇的日期和時間綁定到 date 變量。

- type="datetime": 表明此日期選擇器可以同時選擇日期和時間。

- format="YYYY-MM-DD HH:mm": 定義日期和時間的顯示格式。

- time-format="HH:mm": 單獨定義時間部分的顯示格式。

- v-bind="pickerOptions": 將 pickerOptions 對象的屬性綁定到 el-date-picker 組件上,以實現(xiàn)對日期和時間選擇的限制。

腳本部分 (<script>)

1. 導入和響應式數(shù)據(jù)聲明

- import { ref, computed } from 'vue': 從 Vue 3 的 vue 包中導入 refcomputed 函數(shù)。

- const date = ref(''): 創(chuàng)建一個響應式的 date 變量,用于存儲用戶選擇的日期和時間。初始值為空字符串。

2. makeRange 函數(shù)

const makeRange = (start: number, end: number) => {
  const result: number[] = [];
  for (let i = start; i <= end; i++) {
    result.push(i);
  }
  return result;
}
  • 此函數(shù)接收兩個數(shù)字參數(shù) startend。
  • 創(chuàng)建一個空數(shù)組 result。
  • 通過 for 循環(huán)將從 startend 的數(shù)字添加到 result 數(shù)組中。

3. disabledHours 函數(shù)

const disabledHours = () => {
  let newVal = new Date(date.value);
  if (newVal && newVal.getFullYear() == new Date().getFullYear() &&
    newVal.getMonth() == new Date().getMonth() &&
    newVal.getDate() == new Date().getDate()
  ) {
    return makeRange(0, new Date().getHours() - 1);
  }
}
  • 創(chuàng)建一個 newVal 對象,通過 date.value 來初始化。
  • 檢查 newVal 是否為今天的日期。
  • 如果是今天,使用 makeRange 函數(shù)生成一個數(shù)組,包含從 0 到當前小時減 1 的小時數(shù),表示這些小時不可選。

4.disabledMinutes 函數(shù)

const disabledMinutes = (hour: number) => {
  let newVal = new Date(date.value);
  if (newVal && newVal.getFullYear() == new Date().getYear() &&
    newVal.getMonth() == new Date().getMonth() &&
    newVal.getDate() == new Date().getDate()
  ) {
    return makeRange(0, new Date().getMinutes() - 1);
  }
}
  • disabledHours 函數(shù)類似,但是它接收一個 hour 參數(shù)(此處似乎未使用)。
  • 檢查是否為今天,如果是,則使用 makeRange 函數(shù)生成一個數(shù)組,包含從 0 到當前分鐘減 1 的分鐘數(shù),表示這些分鐘不可選。

5. pickerOptions 計算屬性

const pickerOptions = computed(() => {
  return {
    // 限制今天之前的日期不能選擇
    disabledDate(time: any) {
      return time.getTime() < Date.now() - 8.64e7;
    },
    // 限制今天之前的小時不能選擇
    disabledHours,
    // 限制今天之前的分鐘不能選擇
    disabledMinutes
  };
});
  • computed(() => {...}): 這是一個計算屬性,返回一個對象,包含對日期選擇器的配置選項。
  • disabledDate(time: any) {...}: 接收一個 time 對象,將其轉(zhuǎn)換為時間戳,如果小于當前時間戳減去一天的毫秒數(shù)(8.64e7 毫秒是一天),則該日期不可選。
  • disabledHours: 引用 disabledHours 函數(shù),用于限制某些小時不可選。
  • disabledMinutes: 引用 disabledMinutes 函數(shù),用于限制某些分鐘不可選。

三、效果展示

到此這篇關于el-date-picker日期時間選擇器的選擇時間限制到分鐘級別的文章就介紹到這了,更多相關el-date-picker選擇時間到分內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue如何動態(tài)給data中添加變量

    vue如何動態(tài)給data中添加變量

    這篇文章主要介紹了vue如何動態(tài)給data中添加變量問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue實現(xiàn)帶自動吸附功能的懸浮球

    vue實現(xiàn)帶自動吸附功能的懸浮球

    這篇文章主要為大家詳細介紹了vue實現(xiàn)帶自動吸附功能的懸浮球,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue + axios get下載文件功能

    vue + axios get下載文件功能

    這篇文章主要為大家詳細介紹了vue + axios get下載文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Vue3 響應式數(shù)據(jù) reactive使用方法

    Vue3 響應式數(shù)據(jù) reactive使用方法

    這篇文章主要介紹了Vue3 響應式數(shù)據(jù) reactive使用方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-11-11
  • Vue3.5中響應式Props解構的編譯原理

    Vue3.5中響應式Props解構的編譯原理

    在Vue3.5版本中,響應式Props的解構功能正式轉(zhuǎn)正,該功能允許即使在解構后也不丟失響應性,文通過編譯階段的處理,如何保持解構后的props變量仍保持響應性,編譯過程中的defineProps宏函數(shù)處理,通過AST和上下文操作實現(xiàn)變量替換,從而讓解構后的變量在運行時維持響應式狀態(tài)
    2024-09-09
  • Vue前端打包的詳細流程

    Vue前端打包的詳細流程

    這篇文章主要介紹了Vue前端打包的詳細流程,下面文章圍繞Vue前端打包的相關資料展開詳細內(nèi)容,需要的小伙伴可以參考一下,希望對大家有所幫助
    2021-11-11
  • Vue中指令v-model的原理及使用方法

    Vue中指令v-model的原理及使用方法

    v-model是Vue中的一個重要語法糖,主要用于實現(xiàn)數(shù)據(jù)的雙向綁定,它通過結合value屬性和input事件,簡化了代碼并提高了開發(fā)效率,文中通過代碼介紹的非常詳解,需要的朋友可以參考下
    2024-09-09
  • vue 配置多頁面應用的示例代碼

    vue 配置多頁面應用的示例代碼

    這篇文章主要介紹了vue 配置多頁面應用的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • vue init失敗簡單解決方法(終極版)

    vue init失敗簡單解決方法(終極版)

    這篇文章主要介紹了vue init失敗的簡單解決方法-終極版,需要的朋友可以參考下
    2017-12-12
  • vue2使用element-ui,el-table不顯示,用npm安裝方式

    vue2使用element-ui,el-table不顯示,用npm安裝方式

    這篇文章主要介紹了vue2使用element-ui,el-table不顯示,用npm安裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評論