DateTimePicker 日期时间选择
在一个输入框中同时选择日期和时间,确认后把结果写回 time.Time。
何时使用
- 预约、发布、截止时间等需要精确到时分秒的单个时间点。
- 需要限制工作日、工作时段,或提供“明早”“月底”等快捷时间时。
- 只选日期用 DatePicker 日期选择;只选时间用 TimePicker 时间选择。
- 需要两个时间点组成区间时,用 RangePicker 或 TimeRangePicker 时间范围。
在线体验
基本用法
import (
"time"
"github.com/tangoui/tango/ui"
)
var publishAt = time.Date(2026, 1, 15, 9, 30, 0, 0, time.UTC)
func page(c *ui.Context) {
ui.DateTimePicker(c, &publishAt).A11yLabel("发布时间")
}DateTimePicker 绑定 *time.Time;零值表示没有选择。面板中的日期和时间先作为草稿,点击“确定”或按 Enter 后才提交。
常见用法
自定义格式与步长
Format 使用 Go 的时间布局,默认为 "2006-01-02 15:04:05",省略秒会隐藏秒列。Steps 分别设置时、分、秒列的步长。DefaultTime(t) 在选日期时为尚未指定的时刻提供默认值,只读取 t 的时、分、秒,默认为 00:00:00。ShowNow(false) 隐藏页脚的“现在”。
import (
"time"
"github.com/tangoui/tango/ui"
)
var startAt time.Time
func page(c *ui.Context) {
ui.DateTimePicker(c, &startAt).
Format("2006-01-02 15:04").
Steps(1, 15, 0).
Width(240).
Placeholder("选择开始时间")
}禁用日期和时间
DisabledDate 收到 UTC 午夜时间;DisabledTime 收到当前草稿日期,并返回需要禁用的时、分、秒。禁用规则也会阻止手动输入或预设值提交。
MinDate(t) 和 MaxDate(t) 同时约束可提交的日期和面板翻月范围。
import (
"time"
"github.com/tangoui/tango/ui"
)
var meetingAt time.Time
func page(c *ui.Context) {
ui.DateTimePicker(c, &meetingAt).
DisabledDate(func(day time.Time) bool {
return day.Weekday() == time.Saturday || day.Weekday() == time.Sunday
}).
DisabledTime(func(day time.Time) ui.DisabledTimes {
return ui.DisabledTimes{
Hours: func(h int) bool { return h < 9 || h > 18 },
Minutes: func(h, m int) bool { return h == 18 && m != 0 },
}
})
}快捷时间
Presets 接受 DatePreset。填写 Value 表示单个时间点;Range 也可用于兼容范围预设,但 DateTimePicker 只采用预设的第一个值。
import (
"time"
"github.com/tangoui/tango/ui"
)
var dueAt = time.Time{}
func page(c *ui.Context) {
ui.DateTimePicker(c, &dueAt).Presets(
ui.DatePreset{Label: "明早", Value: time.Date(2026, 1, 16, 9, 0, 0, 0, time.UTC)},
ui.DatePreset{Label: "月底", Value: time.Date(2026, 1, 31, 18, 0, 0, 0, time.UTC)},
)
}状态、清除与提交反馈
Status("error") 或 Status("warning") 改变输入框反馈;AllowClear(true) 显示清除操作。Changed 只在本帧确认或清除后返回 true。
import (
"time"
"github.com/tangoui/tango/ui"
)
var deadline time.Time
func page(c *ui.Context) {
picker := ui.DateTimePicker(c, &deadline).
Status("warning").AllowClear(true).Open()
if picker.Changed() {
ui.Message(c, "success", "时间已更新")
}
}主题与样式
Size(ui.Small)、默认尺寸和Size(ui.Large)控制输入框尺寸;Variant("outlined" | "filled" | "borderless" | "underlined")控制外框。DateTimePicker使用日期、时间输入组件的主题令牌;Ant Design、Element Plus、shadcn 会分别调整面板、输入列和确认区的密度。ConfigProvider.Defaults("DateTimePicker", ...)支持format、placeholder、allowClear、width、size、variant。
注意事项
- 把
time.Time放在页面函数外的稳定状态中;即时模式每帧重建控件,但不会替你保存业务状态。 Format是 Go 时间布局,不是前端常见的YYYY-MM-DD写法,例如分钟必须写04。DisabledDate与DisabledTime会同时约束点击、键盘输入和预设值;回调中的日期按 UTC 午夜传入。- 输入框和面板支持键盘操作;没有可见表单标签时设置
A11yLabel。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
DateTimePicker 禁用日期 / 时间与快捷选项
gallery 演示 DateTimePickerDisabled 的 Render 函数,源码 page_date_more.go#L71。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.DateTimePicker
func DateTimePicker(c *Context, val *time.Time) *DateTimePickerWDateTimePicker adds a date-time input bound to *val (zero = empty).
返回 *DateTimePickerW,可链式调用的方法见 DateTimePickerW 方法。
DateTimePickerW 方法
ui.DateTimePickerW:DateTimePickerW is an input whose dropdown combines a month calendar with time columns; the value is committed with "确定" or Enter.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort). | A11yLabel(s string) *DateTimePickerW |
AllowClear | AllowClear(on bool) *DateTimePickerW | |
Changed | Changed reports a committed value this frame. | Changed() bool |
DefaultTime | DefaultTime is the clock given to a day picked while no time is set (Element default-time, Ant showTime.defaultValue; default 00:00:00). | DefaultTime(t time.Time) *DateTimePickerW |
Disabled | Disabled disables the picker. | Disabled(on bool) *DateTimePickerW |
DisabledDate | DisabledDate marks days that cannot be picked (called with UTC midnights); typed or preset values on such a day are not committed. | DisabledDate(fn func(time.Time) bool) *DateTimePickerW |
DisabledTime | DisabledTime disables hours, minutes and seconds per day (Ant disabledTime): fn receives the drafted day (UTC midnight) and returns the options to grey out in the time columns. | DisabledTime(fn func(day time.Time) DisabledTimes) *DateTimePickerW |
Format | Format sets the Go layout (default "2006-01-02 15:04:05"); omitting the seconds hides the second column. | Format(f string) *DateTimePickerW |
IsOpen | IsOpen reports whether the dropdown is shown. | IsOpen() bool |
Layout | Layout(o ...layout.Option) *DateTimePickerW | |
MaxDate | MaxDate limits the pickable days (Ant maxDate), like MinDate. | MaxDate(t time.Time) *DateTimePickerW |
MinDate | MinDate limits the pickable days (Ant minDate): earlier days cannot be picked, typed or confirmed and the month arrows stop at t's month. | MinDate(t time.Time) *DateTimePickerW |
Open | Open forces the dropdown open (screenshots, tests). | Open() *DateTimePickerW |
Placeholder | Placeholder(s string) *DateTimePickerW | |
Placement | Placement sets where the dropdown opens: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); it flips when there is no room. | Placement(p Placement) *DateTimePickerW |
Presets | Presets adds a side list of named date-times (DatePreset.Value, else Range[0]) that commit immediately. | Presets(p ...DatePreset) *DateTimePickerW |
ShowNow | ShowNow toggles the Now action in the footer (Ant showNow, on by default). | ShowNow(on bool) *DateTimePickerW |
Size | Size(s ControlSize) *DateTimePickerW | |
Status | Status(s string) *DateTimePickerW | |
Steps | Steps(h, m, s int) *DateTimePickerW | |
Variant | Variant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined". | Variant(v string) *DateTimePickerW |
Width | Width(px float32) *DateTimePickerW |