帮酷LOGO
0 0 评论
  • 显示原文与译文双语对照的内容
文章标签:golang  tool  工具箱  Time T  时间  
Now is a time toolkit for golang

  • 源代码名称:now
  • 源代码网址:http://www.github.com/jinzhu/now
  • now源代码文档
  • now源代码下载
  • Git URL:
    git://www.github.com/jinzhu/now.git
  • Git Clone代码到本地:
    git clone http://www.github.com/jinzhu/now
  • Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/jinzhu/now
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
  • 现在是golang的时间工具箱

    wercker status

    安装

    
    go get -u github.com/jinzhu/now
    
    
    
    

    用法

    根据当前时间计算时间

    import"github.com/jinzhu/now"time.Now() // 2013-11-18 17:51:49.123456789 Monnow.BeginningOfMinute() // 2013-11-18 17:51:00 Monnow.BeginningOfHour() // 2013-11-18 17:00:00 Monnow.BeginningOfDay() // 2013-11-18 00:00:00 Monnow.BeginningOfWeek() // 2013-11-17 00:00:00 Sunnow.BeginningOfMonth() // 2013-11-01 00:00:00 Frinow.BeginningOfQuarter() // 2013-10-01 00:00:00 Tuenow.BeginningOfYear() // 2013-01-01 00:00:00 Tuenow.WeekStartDay = time.Monday// Set Monday as first day, default is Sundaynow.BeginningOfWeek() // 2013-11-18 00:00:00 Monnow.EndOfMinute() // 2013-11-18 17:51:59.999999999 Monnow.EndOfHour() // 2013-11-18 17:59:59.999999999 Monnow.EndOfDay() // 2013-11-18 23:59:59.999999999 Monnow.EndOfWeek() // 2013-11-23 23:59:59.999999999 Satnow.EndOfMonth() // 2013-11-30 23:59:59.999999999 Satnow.EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tuenow.EndOfYear() // 2013-12-31 23:59:59.999999999 Tuenow.WeekStartDay = time.Monday// Set Monday as first day, default is Sundaynow.EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun

    基于其他时间计算时间

    t:= time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
    now.New(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu

    星期一/星期日

    不要干扰 WeekStartDay 设置,你可以使用 MondaySunday

    now.Monday() // 2013-11-18 00:00:00 Monnow.Sunday() // 2013-11-24 00:00:00 Sun (Next Sunday)now.EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun (End of next Sunday)t:= time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.Now().Location()) // 2013-11-24 17:51:49.123456789 Sunnow.New(t).Monday() // 2013-11-18 00:00:00 Sun (Last Monday if today is Sunday)now.New(t).Sunday() // 2013-11-24 00:00:00 Sun (Beginning Of Today if today is Sunday)now.New(t).EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun (End of Today if today is Sunday)

    将字符串解析为时间

    time.Now() // 2013-11-18 17:51:49.123456789 Mon// Parse(string) (time.Time, error)t, err:= now.Parse("2017") // 2017-01-01 00:00:00, nilt, err:= now.Parse("2017-10") // 2017-10-01 00:00:00, nilt, err:= now.Parse("2017-10-13") // 2017-10-13 00:00:00, nilt, err:= now.Parse("1999-12-12 12") // 1999-12-12 12:00:00, nilt, err:= now.Parse("1999-12-12 12:20") // 1999-12-12 12:20:00, nilt, err:= now.Parse("1999-12-12 12:20:21") // 1999-12-12 12:20:00, nilt, err:= now.Parse("10-13") // 2013-10-13 00:00:00, nilt, err:= now.Parse("12:20") // 2013-11-18 12:20:00, nilt, err:= now.Parse("12:20:13") // 2013-11-18 12:20:13, nilt, err:= now.Parse("14") // 2013-11-18 14:00:00, nilt, err:= now.Parse("99:99") // 2013-11-18 12:20:00, Can't parse string as time: 99:99// MustParse must parse string to time or it will panicnow.MustParse("2013-01-13") // 2013-01-13 00:00:00now.MustParse("02-17") // 2013-02-17 00:00:00now.MustParse("2-17") // 2013-02-17 00:00:00now.MustParse("8") // 2013-11-18 08:00:00now.MustParse("2002-10-12 22:14") // 2002-10-12 22:14:00now.MustParse("99:99") // panic: Can't parse string as time: 99:99

    扩展 now 以支持更多格式非常简单,只需更新与它的他时间布局 e.g的now.TimeFormats:

    now.TimeFormats = append(now.TimeFormats, "02 Jan 2006 15:04")

    如果你想要正式支持格式,请向我发送请求请求

    你可以帮助使项目更好,查看 http://gorm.io/contribute.html 你可以做的事。

    作者

    jinzhu

    许可证

    MIT许可协议下发布。



    文章标签:tool  时间  工具箱  golang  Time T  

    Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语