Go timestamp and date string conversion to each other

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

In the go language, get the timestamp with time. Now (). Unix (), formatted time with T. Format, time for parsing. Parse.

Look at the instance code:

Package main Import ("FMT"" Time") Func main () {//Get time stampTimestamp:=Time . Now (). Unix () fmt. PRINTLN (timestamp)//formatted as a string, TM is of type timeTM:= time. Unix (timestamp,0) fmt. Println (TM. Format ("2006-01-02 03:04:05 PM") FMT. Println (TM. Format ("02/01/2006 15:04:05 PM"))  //From a string to a timestamp , the first argument is the format, the second is the time string to converttm2, _:= time. Parse ("01/02/2006","02/08/2015") fmt. Println (TM2. Unix ())}

Output Result:

1423361979

2015-02-08 10:19:39 AM

08/02/2015 10:19:39 AM

1423353600

Looking at the code above, you might wonder why the format string was used in the 2006-01-02 format. In the go language, these numbers are special letters, not random numbers, see the following list:

Month 1,01,jan,january

Day 2,02,_2

When 3,03,15,pm,pm,am,am

Sub 4,04

SEC 5,05

Year 06,2006

Weeks Mon,monday

Time zone difference means -07,-0700,z0700,z07:00,-07:00,mst

Time zone letter abbreviation MST

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.