diff --git a/timex/template.go b/timex/template.go index 85eead70b..e89d039bf 100644 --- a/timex/template.go +++ b/timex/template.go @@ -8,13 +8,13 @@ import ( // some common datetime templates const ( - DefaultTemplate = "Y-m-d H:i:s" - TemplateWithMs3 = "Y-m-d H:i:s.v" // end with ".000" - TemplateWithMs6 = "Y-m-d H:i:s.u" // end with ".000000" + DefaultTemplate = "Y-m-d H:I:S" // equals layout: "2006-01-02 15:04:05" + TemplateWithMs3 = "Y-m-d H:I:S.v" // end with ".000" + TemplateWithMs6 = "Y-m-d H:I:S.u" // end with ".000000" ) // char to Go date layout -// eg: "Y-m-d H:i:s" => "2006-01-02 15:04:05", +// eg: "Y-m-d H:I:S" => "2006-01-02 15:04:05", // // # More see time.stdLongMonth // diff --git a/timex/util.go b/timex/util.go index 9da093e4c..c6e3c9a2c 100644 --- a/timex/util.go +++ b/timex/util.go @@ -9,6 +9,11 @@ import ( // NowUnix is short of time.Now().Unix() func NowUnix() int64 { return time.Now().Unix() } +// NowDate quick get current date string. if template is empty, will use DefaultTemplate. +func NowDate(template ...string) string { + return FormatByTpl(time.Now(), basefn.FirstOr(template, DefaultTemplate)) +} + // Format convert time to string use default layout func Format(t time.Time) string { return t.Format(DefaultLayout) } diff --git a/timex/util_test.go b/timex/util_test.go index f9e6a658c..59624a00a 100644 --- a/timex/util_test.go +++ b/timex/util_test.go @@ -42,6 +42,7 @@ func TestNowAddDay(t *testing.T) { func TestDateFormat(t *testing.T) { now := time.Now() + assert.Eq(t, now.Format("2006-01-02 15:04:05"), timex.NowDate()) tests := []struct{ layout, template string }{ {"20060102 15:04:05", "Ymd H:I:S"},