Skip to content

TimeRangePicker 时间范围 ​

同时选择开始和结束时间,适合班次、营业时间和同一天内的时段配置。

何时使用 ​

在线体验 ​

TimeRangePicker 时间范围 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import (
    "time"

    "github.com/tangoui/tango/ui"
)

var officeHours = [2]time.Time{
    time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC),
    time.Date(2026, 1, 1, 18, 0, 0, 0, time.UTC),
}

func page(c *ui.Context) {
    ui.TimeRangePicker(c, &officeHours).Format("15:04").A11yLabel("营业时间")
}

值是 [2]time.Time{开始, 结束};零值表示对应一端尚未选择,确认后组件会按时钟顺序提交区间。

常见用法 ​

设置步长与 12 小时制 ​

HourStep、MinuteStep、SecondStep 控制三列的间隔。Use12Hours 配合 Format("03:04 PM") 显示 AM/PM。

go
import (
    "time"

    "github.com/tangoui/tango/ui"
)

var supportHours [2]time.Time

func page(c *ui.Context) {
    ui.TimeRangePicker(c, &supportHours).
        Use12Hours().Format("03:04 PM").MinuteStep(15)
}

禁用时间 ​

DisabledHours、DisabledMinutes 和 DisabledSeconds 收到 24 小时制的时、分、秒,即使界面使用 12 小时制也按 0–23 判断。

go
import (
    "time"

    "github.com/tangoui/tango/ui"
)

var quietHours [2]time.Time

func page(c *ui.Context) {
    ui.TimeRangePicker(c, &quietHours).
        Format("15:04").
        DisabledHours(func(h int) bool { return h < 8 || h >= 22 }).
        DisabledMinutes(func(h, m int) bool { return h == 21 && m > 30 }).
        HideDisabledOptions(true)
}

占位符、状态与清除 ​

两端可以分别设置占位文字;Status 用于表单错误反馈,AllowClear(false) 关闭悬停清除。

go
import (
    "time"

    "github.com/tangoui/tango/ui"
)

var deployWindow [2]time.Time

func page(c *ui.Context) {
    picker := ui.TimeRangePicker(c, &deployWindow).
        Placeholder("开始", "结束").
        Status("error").
        AllowClear(false)
    if picker.Changed() {
        ui.Message(c, "info", "部署窗口已提交")
    }
}

键盘与展开状态 ​

Open 适合截图、引导或需要首帧展开的场景;真实交互中用户可点击输入框或按 Enter 打开,左右键切换开始/结束列,Enter 提交,Escape 关闭。

go
import (
    "time"

    "github.com/tangoui/tango/ui"
)

var reviewWindow [2]time.Time

func page(c *ui.Context) {
    picker := ui.TimeRangePicker(c, &reviewWindow).Open()
    ui.Text(c, "已展开:"+boolText(picker.IsOpen()))
}

func boolText(on bool) string {
    if on {
        return "是"
    }
    return "否"
}

主题与样式 ​

  • Size(ui.Small)、默认尺寸和 Size(ui.Large) 控制触发器;Variant("outlined" | "filled" | "borderless" | "underlined") 控制外观。
  • 主题会分别调整两个时间段、分隔符、时间列和禁用项的密度;HideDisabledOptions(true) 只改变禁用项的呈现,不改变规则。
  • ConfigProvider.Defaults("TimeRangePicker", ...) 支持 format、separator、use12Hours、步长、allowClear、width、size、variant 和 placeholder。

注意事项 ​

  • 绑定值应放在稳定状态中;即时模式下组件每帧重建,但 [2]time.Time 的值由调用方保存。
  • Format 是 Go 时间布局;使用 12 小时制时省略 Format 也会使用默认的 AM/PM 布局。
  • 禁用规则按 24 小时制执行;无效或被禁用的手动输入不会提交。
  • 没有表单标签时设置 A11yLabel;组件的开始/结束占位符会作为无障碍名称的后备文本。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

TimeRangePicker 时间范围

gallery 演示 TimeRangePicker 的 Render 函数,源码 page_p2a_date.go#L87。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.TimeRangePicker ​

go
func TimeRangePicker(c *Context, val *[2]time.Time) *TimeRangePickerW

TimeRangePicker adds a time range input bound to *val ([start, end], zero = empty). Both ends are edited in side-by-side columns and committed with OK (or Enter); the result is ordered by clock (see Order). Each end can also be typed: printable keys edit the active end, Enter applies valid text (and commits once both ends are set), invalid or disabled text is dropped.

ui.TimeRangePicker(c, &span).Format("15:04").MinuteStep(15)

ConfigProvider.Defaults("TimeRangePicker", ...) keys: "format", "separator", "use12Hours", "hourStep", "minuteStep", "secondStep", "allowClear", "width", "size", "variant", "hideDisabledOptions", "placeholder" ([2]string), "placement", "order".

返回 *TimeRangePickerW,可链式调用的方法见 TimeRangePickerW 方法。

TimeRangePickerW 方法 ​

ui.TimeRangePickerW:TimeRangePickerW selects a start and an end time.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used; otherwise the two placeholders).A11yLabel(s string) *TimeRangePickerW
AllowClearAllowClear(on bool) *TimeRangePickerW
ChangedChanged reports a committed range this frame.Changed() bool
DisabledDisabled disables the picker.Disabled(on bool) *TimeRangePickerW
DisabledHoursDisabledHours marks hours (0-23) that cannot be picked.DisabledHours(fn func(h int) bool) *TimeRangePickerW
DisabledMinutesDisabledMinutes marks minutes of hour h that cannot be picked.DisabledMinutes(fn func(h, m int) bool) *TimeRangePickerW
DisabledSecondsDisabledSeconds marks seconds of h:m that cannot be picked.DisabledSeconds(fn func(h, m, s int) bool) *TimeRangePickerW
FormatFormat sets the Go time layout ("15:04:05" default, "15:04", or with Use12Hours "03:04 PM").Format(f string) *TimeRangePickerW
HideDisabledOptionsHideDisabledOptions leaves disabled hours, minutes and seconds out of the columns instead of greying them (Ant hideDisabledOptions).HideDisabledOptions(on bool) *TimeRangePickerW
HourStepHourStep(n int) *TimeRangePickerW
IsOpenIsOpen reports whether the dropdown is shown.IsOpen() bool
LayoutLayout(o ...layout.Option) *TimeRangePickerW
MinuteStepMinuteStep(n int) *TimeRangePickerW
OpenOpen forces the dropdown open (screenshots, tests).Open() *TimeRangePickerW
OrderOrder toggles sorting the two ends by clock (Ant order, on by default).Order(on bool) *TimeRangePickerW
PlaceholderPlaceholder(start, end string) *TimeRangePickerW
PlacementPlacement sets where the columns open: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); they flip when there is no room.Placement(p Placement) *TimeRangePickerW
SecondStepSecondStep(n int) *TimeRangePickerW
SeparatorSeparator sets the text between the two ends (Ant separator, Element range-separator; default the Locale's RangeSeparator).Separator(s string) *TimeRangePickerW
SizeSize(s ControlSize) *TimeRangePickerW
StatusStatus(s string) *TimeRangePickerW
Use12HoursUse12Hours shows 12-hour columns with AM / PM.Use12Hours() *TimeRangePickerW
VariantVariant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined".Variant(v string) *TimeRangePickerW
WidthWidth(px float32) *TimeRangePickerW

相关链接 ​

以 MIT 许可发布