Skip to content

DatePicker 日期选择框 ​

输入或选择日期的控件。除了单个日期 DatePicker,还提供日期范围 RangePicker、多选日期 MultiDatePicker 和日期时间 DateTimePicker,并支持周 / 月 / 季度 / 年等粒度、预设快捷项和禁用日期。

何时使用 ​

  • 需要输入一个日期(生日、截止日期)或一个时间段(统计区间、请假时间)时。
  • 只需要一天中的时间,不需要日期时,用 TimePicker 时间选择框。
  • 需要在页面上直接展示整月日程或事件时,用 Calendar 日历。

在线体验 ​

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

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import (
	"time"

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

var deadline time.Time

func page(c *ui.Context) {
	ui.DatePicker(c, &deadline).Placeholder("请选择截止日期")
	if !deadline.IsZero() {
		ui.Text(c, "截止:"+deadline.Format("2006-01-02"))
	}
}

值绑定到 *time.Time,零值表示未选择。点击或按 Enter 打开面板,方向键移动、PageUp / PageDown 翻月、Enter 选中;也可以直接在输入框中键入日期,Enter 或离开输入框时生效。选出的值是 UTC 零点。Format 设置显示格式(Go 时间布局,默认 "2006-01-02")。

常见用法 ​

选择器粒度 ​

Picker 切换粒度:ui.PickerWeek、ui.PickerMonth、ui.PickerQuarter、ui.PickerYear。值为对应周期的第一天(UTC 零点)。

ShowWeek(true) 在日视图显示 ISO 周数,周选择器始终显示这一列。ShowNow(false) 隐藏面板底部的“今天”。

go
import (
	"time"

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

var week, month, quarter, year time.Time

func page(c *ui.Context) {
	ui.DatePicker(c, &week).Picker(ui.PickerWeek)
	ui.DatePicker(c, &month).Picker(ui.PickerMonth).Format("2006-01")
	ui.DatePicker(c, &quarter).Picker(ui.PickerQuarter)
	ui.DatePicker(c, &year).Picker(ui.PickerYear)
}

日期范围 RangePicker ​

RangePicker 绑定 *[2]time.Time。第一次点击选一端,第二次点击选另一端,结果默认自动排序。Order(false) 按选择顺序保存两端。Placeholder(开始, 结束) 设置两端占位符,Separator(s) 设置两端之间的分隔文字。日粒度下 ShowTime() 在面板下方加入开始 / 结束时间列,点“确定”后提交。

go
import (
	"time"

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

var (
	span     [2]time.Time
	shift    [2]time.Time
	quarters [2]time.Time
)

func page(c *ui.Context) {
	ui.RangePicker(c, &span).Placeholder("开始日期", "结束日期")
	ui.RangePicker(c, &shift).ShowTime().Format("2006-01-02 15:04")
	ui.RangePicker(c, &quarters).Picker(ui.PickerQuarter)
}

预设快捷项 ​

Presets(ui.DatePreset{...}) 在面板侧边显示命名的快捷项,点击后立即选中。DatePicker / DateTimePicker 使用 Value,RangePicker 使用 Range。

go
import (
	"time"

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

var (
	day    time.Time
	period [2]time.Time
)

func page(c *ui.Context) {
	now := time.Now().UTC().Truncate(24 * time.Hour)
	ui.DatePicker(c, &day).Presets(
		ui.DatePreset{Label: "今天", Value: now},
		ui.DatePreset{Label: "昨天", Value: now.AddDate(0, 0, -1)},
		ui.DatePreset{Label: "一周前", Value: now.AddDate(0, 0, -7)},
	)
	ui.RangePicker(c, &period).Presets(
		ui.DatePreset{Label: "最近 7 天", Range: [2]time.Time{now.AddDate(0, 0, -7), now}},
		ui.DatePreset{Label: "最近 30 天", Range: [2]time.Time{now.AddDate(0, 0, -30), now}},
	)
}

禁用日期与时间 ​

DisabledDate(func(time.Time) bool) 返回 true 的日期不可选也不可键入(使用 Picker 时传入的是周期的第一天)。带时间的选择器还可以用 DisabledTime 返回 ui.DisabledTimes,按天禁用小时、分钟、秒;RangePicker 的回调多一个 end 参数区分开始和结束端。

DatePicker、RangePicker 和 DateTimePicker 都可用 MinDate(t)、MaxDate(t) 限制可选范围及面板翻页范围。Placement 设置面板的弹出位置,默认 ui.BottomStart,空间不足时自动翻转。

go
import (
	"time"

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

var (
	visit   time.Time
	meeting time.Time
)

func weekend(d time.Time) bool { return d.Weekday() == time.Saturday || d.Weekday() == time.Sunday }

func page(c *ui.Context) {
	ui.DatePicker(c, &visit).DisabledDate(weekend)
	ui.DateTimePicker(c, &meeting).DisabledDate(weekend).Format("2006-01-02 15:04").
		DisabledTime(func(day time.Time) ui.DisabledTimes {
			return ui.DisabledTimes{Hours: func(h int) bool { return h < 9 || h >= 18 }}
		})
}

日期时间 DateTimePicker ​

DateTimePicker 在日期面板旁边加上时 / 分 / 秒列,绑定 *time.Time。Steps(时, 分, 秒) 设置步长,Format 中不含秒时隐藏秒列。

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, 1).Placeholder("开始时间")
}

多选日期 MultiDatePicker ​

MultiDatePicker 绑定 *[]time.Time:每次点击切换一个日期,面板保持打开,选中项按时间排序并以标签显示(Backspace 删除最后一个)。MaxTagCount 折叠多余标签,NeedConfirm(true) 让改动在点“确定”后才生效。

go
import (
	"time"

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

var dutyDays, months []time.Time

func page(c *ui.Context) {
	ui.MultiDatePicker(c, &dutyDays).MaxTagCount(3).A11yLabel("值班日期")
	ui.MultiDatePicker(c, &months).Picker(ui.PickerMonth).NeedConfirm(true).A11yLabel("多个月份")
}

需要确认、状态与外观 ​

NeedConfirm(true) 让选中的日期先作为草稿,点“确定”(或在同一天再按 Enter)才提交,关闭面板则丢弃。AllowClear(false) 去掉悬停时的清除按钮;Status("error" | "warning")、Variant("filled" | "borderless" | "underlined")、Size、Width 调整外观。

go
import (
	"time"

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

var signedAt time.Time

func page(c *ui.Context) {
	ui.DatePicker(c, &signedAt).NeedConfirm(true)
	ui.DatePicker(c, &signedAt).Status("error").AllowClear(false).Width(240)
	ui.DatePicker(c, &signedAt).Variant("filled").Size(ui.Large)
}

主题与样式 ​

  • 三套风格共用同一套面板结构,单元格尺寸、圆角、选中色和范围高亮色来自各自的设计令牌;切换风格无需改代码。
  • 组件令牌:theme.Components.DatePicker(DatePickerToken),包含单元格尺寸(CellWidth、CellHeight)、圆角(CellRadius)以及悬停、选中、范围、今天、禁用等状态颜色(CellActiveBg、CellRangeBg、CellTodayBorder…)。
  • ConfigProvider.Defaults("DatePicker", ui.Props{...}) 支持 format、placeholder、picker(ui.PickerType)、allowClear、width、size、variant;"RangePicker" 另有 format、separator、allowClear、showTime、width、size、variant、picker、placeholder([2]string);"DateTimePicker"、"TimeRangePicker" 也各有自己的默认值键。

注意事项 ​

  • 时区:面板选出的日期是 UTC 零点。展示给用户或与本地时间比较时,先按需要转换时区,避免跨日偏差。
  • 状态:值就是你绑定的 time.Time / [2]time.Time / []time.Time;面板是否展开、正在浏览的月份等由组件按调用位置保存。在循环中渲染多个日期框时用 ui.Key 包住每一行。
  • 表单校验:放进 Form 时用 ui.RulesOf(f.Item("截止日期"), &deadline, ui.CheckValue(func(t time.Time) string { ... })),或用 RulesFunc 把日期格式化成字符串后使用 ui.Required。
  • 无障碍:在表单项里自动以表单项标签命名,否则依次使用 A11yLabel、占位符。面板可完全用键盘操作。
  • 截图与测试:Open() 强制展开面板,只用于截图和测试,不要在正式界面里常驻调用。

示例代码 ​

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

DatePicker 日期选择框

gallery 演示 DatePicker 的 Render 函数,源码 page_entry_date.go#L44。

加载中…

DatePicker 可选范围、周数与弹出位置

gallery 演示 DatePickerAlign 的 Render 函数,源码 page_b1d.go#L41。

加载中…

DatePicker 需要确认

gallery 演示 DatePickerConfirm 的 Render 函数,源码 page_date_more.go#L64。

加载中…

DatePicker 月份选择

gallery 演示 DatePickerMonth 的 Render 函数,源码 page_p2a_date.go#L58。

加载中…

DatePicker 多选日期

gallery 演示 DatePickerMultiple 的 Render 函数,源码 page_date_more.go#L51。

加载中…

DatePicker 粒度(周 / 月 / 季度 / 年)

gallery 演示 DatePickerPicker 的 Render 函数,源码 page_p2a_date.go#L41。

加载中…

DatePicker 预设、清除、状态

gallery 演示 DatePickerPresets 的 Render 函数,源码 page_p2a_date.go#L63。

加载中…

API ​

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

入口函数 ​

ui.DatePicker ​

go
func DatePicker(c *Context, val *time.Time) *DatePickerW

DatePicker adds a date input bound to *val. Picked values are UTC midnights (with Picker: the first day of the week, month, quarter or year).

The value can also be typed: printable keys edit the text in place and open the panel, which follows a valid date; Enter (or leaving the field) commits it, invalid or disabled text is dropped.

ConfigProvider.Defaults("DatePicker", ...) keys: "format", "placeholder", "picker" (PickerType), "allowClear", "width", "size", "variant", "placement", "showNow", "showWeek", "needConfirm".

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

DatePickerW 方法 ​

ui.DatePickerW:DatePickerW is an input that opens a date panel (days, or weeks, months, quarters or years with Picker).

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort).A11yLabel(s string) *DatePickerW
AllowClearAllowClear toggles the clear button shown on hover (on by default).AllowClear(on bool) *DatePickerW
ChangedChanged() bool
DisabledDisabled(on bool) *DatePickerW
DisabledDateDisabledDate marks dates that cannot be picked (with Picker: called with the first day of the period).DisabledDate(fn func(time.Time) bool) *DatePickerW
FormatFormat sets the Go layout of the value (date, month and year pickers; week and quarter values use the Locale labels "2026-46th" / "2026-Q4").Format(f string) *DatePickerW
IsOpenIsOpen reports whether the panel is shown.IsOpen() bool
LayoutLayout applies layout options to the input.Layout(o ...layout.Option) *DatePickerW
MaxDateMaxDate limits the pickable dates (Ant maxDate), like MinDate.MaxDate(t time.Time) *DatePickerW
MinDateMinDate limits the pickable dates (Ant minDate): periods wholly before t cannot be picked or typed, and the panel arrows stop at t's page.MinDate(t time.Time) *DatePickerW
NeedConfirmNeedConfirm keeps a picked date as a draft until the footer's OK button (or Enter on the picked cell, or Enter after typing) commits it (Ant needConfirm); closing the panel otherwise drops the draft.NeedConfirm(on bool) *DatePickerW
OpenOpen forces the panel open (screenshots, tests).Open() *DatePickerW
PickerPicker sets the granularity: PickerDate (default), PickerWeek, PickerMonth, PickerQuarter or PickerYear (Ant picker, Element type).Picker(p PickerType) *DatePickerW
PlaceholderPlaceholder(s string) *DatePickerW
PlacementPlacement sets where the panel opens: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement "bottomLeft" / "bottomRight" / "topLeft" / "topRight"); it flips when there is no room.Placement(p Placement) *DatePickerW
PresetsPresets adds a side list of named dates (DatePreset.Value) that pick immediately.Presets(p ...DatePreset) *DatePickerW
ShowNowShowNow toggles the Today link in the panel footer (Ant showNow, on by default for the date picker).ShowNow(on bool) *DatePickerW
ShowWeekShowWeek adds the ISO week-number column to the day grid (Ant showWeek; the week picker always shows it).ShowWeek(on bool) *DatePickerW
SizeSize(s ControlSize) *DatePickerW
StatusStatus sets the validation look: "error" or "warning".Status(s string) *DatePickerW
VariantVariant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined" (Ant variant).Variant(v string) *DatePickerW
WidthWidth sets the input width in pixels (default 180).Width(px float32) *DatePickerW

RangePicker、MultiDatePicker、DateTimePicker 的完整方法见 Go API 参考;它们与 DatePicker 的同名方法含义一致。

相关链接 ​

以 MIT 许可发布