Skip to content

DateTimePicker 日期时间选择 ​

在一个输入框中同时选择日期和时间,确认后把结果写回 time.Time。

何时使用 ​

在线体验 ​

DateTimePicker 日期时间选择 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
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) 隐藏页脚的“现在”。

go
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) 同时约束可提交的日期和面板翻月范围。

go
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 只采用预设的第一个值。

go
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。

go
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 演示 DateTimePicker 的 Render 函数,源码 page_entry_date.go#L78。

加载中…

DateTimePicker 禁用日期 / 时间与快捷选项

gallery 演示 DateTimePickerDisabled 的 Render 函数,源码 page_date_more.go#L71。

加载中…

API ​

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

入口函数 ​

ui.DateTimePicker ​

go
func DateTimePicker(c *Context, val *time.Time) *DateTimePickerW

DateTimePicker 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.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort).A11yLabel(s string) *DateTimePickerW
AllowClearAllowClear(on bool) *DateTimePickerW
ChangedChanged reports a committed value this frame.Changed() bool
DefaultTimeDefaultTime 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
DisabledDisabled disables the picker.Disabled(on bool) *DateTimePickerW
DisabledDateDisabledDate 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
DisabledTimeDisabledTime 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
FormatFormat sets the Go layout (default "2006-01-02 15:04:05"); omitting the seconds hides the second column.Format(f string) *DateTimePickerW
IsOpenIsOpen reports whether the dropdown is shown.IsOpen() bool
LayoutLayout(o ...layout.Option) *DateTimePickerW
MaxDateMaxDate limits the pickable days (Ant maxDate), like MinDate.MaxDate(t time.Time) *DateTimePickerW
MinDateMinDate 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
OpenOpen forces the dropdown open (screenshots, tests).Open() *DateTimePickerW
PlaceholderPlaceholder(s string) *DateTimePickerW
PlacementPlacement sets where the dropdown opens: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); it flips when there is no room.Placement(p Placement) *DateTimePickerW
PresetsPresets adds a side list of named date-times (DatePreset.Value, else Range[0]) that commit immediately.Presets(p ...DatePreset) *DateTimePickerW
ShowNowShowNow toggles the Now action in the footer (Ant showNow, on by default).ShowNow(on bool) *DateTimePickerW
SizeSize(s ControlSize) *DateTimePickerW
StatusStatus(s string) *DateTimePickerW
StepsSteps(h, m, s int) *DateTimePickerW
VariantVariant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined".Variant(v string) *DateTimePickerW
WidthWidth(px float32) *DateTimePickerW

相关链接 ​

以 MIT 许可发布