Go Study notes: Time package use

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

Abstract: Like Python, golang time processing is relatively convenient, following the time processing of the previous python to expand

Golang Time Processing
Related package "Time"

  • Time stamp
    Current time stamp

    fmt.Println(time.Now().Unix())# 1389058332
  • STR format time
    Current format time

    fmt.Println(time.Now().Format("2006-01-02 15:04:05"))  // 这是个奇葩,必须是这个时间点, 据说是go诞生之日, 记忆方法:6-1-2-3-4-5# 2014-01-07 09:42:20
  • timestamp to str format time

    str_time := time. Unix (1389058332, 0). Format ("2006-01-02 15:04:05") fmt. Println (str_time) # 2014-01-07 09:32:12  
  • STR format time-to-timestamp
    This is more trouble.

    the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)unix_time := the_time.Unix()fmt.Println(unix_time)# 389045004

    There is also a way to use time . Parse

    the_time, err := time.Parse("2006-01-02 15:04:05", "2014-01-08 09:04:41")if err == nil {        unix_time := the_time.Unix()fmt.Println(unix_time)}# 1389171881
Transferred from: https://my.oschina.net/1123581321/blog/190942


Package Mainimport (    "FMT"    "Time") Func main () {    T, _: = time. Parse ("2006-01-02 15:04:05", "2016-04-20 16:23:00")    FMT. Println (T.unix ())    y, m, D: = time. Unix (1466344320, 0). Date ()    FMT. Println (Y, M, D)    the string after//format must be 2006-01-02 15:04:05, it is said that go is the fmt that was born this time    . Println (time. Now (). Format ("2006-01-02 15:04:05"))    FMT. Println (time. Now (). Format ("2006-01-02"))    FMT. Println (time. Now (). Format ("20060102"))                                                                                                                                Select {Case    <-time. After (5 * time. Second):        FMT. Println ("after 5 second")    }       c: = time. Tick (Ten * time). Second) for now    : = Range C {         FMT. Println (now)    }   }







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.