TimeRangePicker 时间范围
同时选择开始和结束时间,适合班次、营业时间和同一天内的时段配置。
何时使用
- 需要两个时钟值组成一个时间区间时。
- 需要 12 小时制、固定步长或按小时/分钟禁用选项时。
- 只有一个时间点用 TimePicker 时间选择;固定时间列表用 TimeSelect 时间选择。
- 日期也参与区间时,用 RangePicker 日期范围 的
ShowTime()。
在线体验
基本用法
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。
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 判断。
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) 关闭悬停清除。
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 关闭。
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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.TimeRangePicker
func TimeRangePicker(c *Context, val *[2]time.Time) *TimeRangePickerWTimeRangePicker 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.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used; otherwise the two placeholders). | A11yLabel(s string) *TimeRangePickerW |
AllowClear | AllowClear(on bool) *TimeRangePickerW | |
Changed | Changed reports a committed range this frame. | Changed() bool |
Disabled | Disabled disables the picker. | Disabled(on bool) *TimeRangePickerW |
DisabledHours | DisabledHours marks hours (0-23) that cannot be picked. | DisabledHours(fn func(h int) bool) *TimeRangePickerW |
DisabledMinutes | DisabledMinutes marks minutes of hour h that cannot be picked. | DisabledMinutes(fn func(h, m int) bool) *TimeRangePickerW |
DisabledSeconds | DisabledSeconds marks seconds of h:m that cannot be picked. | DisabledSeconds(fn func(h, m, s int) bool) *TimeRangePickerW |
Format | Format sets the Go time layout ("15:04:05" default, "15:04", or with Use12Hours "03:04 PM"). | Format(f string) *TimeRangePickerW |
HideDisabledOptions | HideDisabledOptions leaves disabled hours, minutes and seconds out of the columns instead of greying them (Ant hideDisabledOptions). | HideDisabledOptions(on bool) *TimeRangePickerW |
HourStep | HourStep(n int) *TimeRangePickerW | |
IsOpen | IsOpen reports whether the dropdown is shown. | IsOpen() bool |
Layout | Layout(o ...layout.Option) *TimeRangePickerW | |
MinuteStep | MinuteStep(n int) *TimeRangePickerW | |
Open | Open forces the dropdown open (screenshots, tests). | Open() *TimeRangePickerW |
Order | Order toggles sorting the two ends by clock (Ant order, on by default). | Order(on bool) *TimeRangePickerW |
Placeholder | Placeholder(start, end string) *TimeRangePickerW | |
Placement | Placement sets where the columns open: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); they flip when there is no room. | Placement(p Placement) *TimeRangePickerW |
SecondStep | SecondStep(n int) *TimeRangePickerW | |
Separator | Separator sets the text between the two ends (Ant separator, Element range-separator; default the Locale's RangeSeparator). | Separator(s string) *TimeRangePickerW |
Size | Size(s ControlSize) *TimeRangePickerW | |
Status | Status(s string) *TimeRangePickerW | |
Use12Hours | Use12Hours shows 12-hour columns with AM / PM. | Use12Hours() *TimeRangePickerW |
Variant | Variant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined". | Variant(v string) *TimeRangePickerW |
Width | Width(px float32) *TimeRangePickerW |