Several usages of Golang time

Source: Internet
Author: User
Tags rfc822
This is a creation in Article, where the information may have evolved or changed.

First of all, Golang language design has a guy called xxnuts, really is a pervert, the time of the original design in the 2006-01-02 15:04:05

In fact, this date is meaningful:

2006-01-02t15:04:05z07:00

The meaning of each number:

1 2 3 4 5 6 7        Month Day seconds time zone


Common methods:

1 totime Format (parsing):

Usage One: Use the number of milliseconds

T: = time. Unix (1362984425, 0) NT: = T.format ("2006-01-02 15:04:05") fmt. Println (NT)

Usage two: Format with fixed string to get time object

Const TIMEFORMAT = "2006-01-02 15:04:05" func testxyz (t *testing. T) {T,err:=time. Parse (TimeFormat, "2013-08-11 11:18:46") l.println (t)}


2 ToString Time formatting formatting

Package Mainimport (    "FMT"    "Time") Func main () {    T: = time. Secondstolocaltime (1305861602)    t.zoneoffset = -4*60*60    fmt. Println (T.format ("2006-01-02 15:04:05-0700"))}//= "2011-05-20 03:20:02-0400"


3 format string type

Of course, if there is no type of formatting you want, then look at the following schedule:


Const (    Ansic       = "Mon Jan _2 15:04:05 2006"     unixdate    = "Mon Jan _2 15:04:05 MST 2006 "    rubydate    =" Mon Jan, 15:04:05-0700 2006 "    RFC822   &N Bsp  = "15:04 MST"//RFC822 with numeric zone RFC850      = "Monday, 02-jan-06 15:04:05 MST" &N Bsp   rfc822z     = "15:04-0700"      RFC1123     = "Mon, Jan 2006 15:04: MST "//RFC1123 with numeric zone RFC3339     =" 2006-01-02t15:04:05z07:00 "    rfc1123z   &NB Sp;= "Mon, 2006 15:04:05-0700"      Rfc3339nano = "2006-01-02t15:04:05.999999999z07:00"//Handy Ti Me stamps. Stamp      = "Jan _2 15:04:05"     Kitchen     = "3:04pm"      Stampmilli = "Jan _2 15:04:05.000"     stampmicro = "Jan _2 15:04:05.000000"     Stampnano  = "Jan _2 15:04:05 .000000000 ") 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.