Golang Time and duration

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport "FMT" import "Time" Func main () {p: = fmt.    PRINTLN//We ll start by getting the current time. Now: = time. Now () p ("time. Now (): ' Now '//You can build a ' time ' struct by providing the//year, month, day, etc.    Times is always associated//with a ' location ', i.e. time zone. Then: = time. Date (one, one, a, a, a, 651387237, time). UTC) P ("time.  Date (): ", then) p (" ================================== ")//can extract the various components of the time//    Value as expected. P ("time. Date (). Year (): "and then." Year ()) p ("time. Date (). Month (): ", then. Month ()) p ("time. Date (). Day (): "and then." Day ()) p ("time. Date (). Hour (): ", then. Hour ()) p ("time. Date (). Minute (): ", then. Minute ()) p ("time. Date (). Second (): ", then. Second ()) p ("time. Date (). Nanosecond (): ", then. Nanosecond ()) p ("time. Date (). Location (): "and then.    Location ())//The Monday-sunday ' Weekday ' is also available. P ("time. Date (). Weekday (): ", then. Weekday ()) p ("==================================")//These methods compare the times, testing if the//first occurs BEFO    Re, after, or at the same time//as the second, respectively. P ("time. Date (). Before (now): "and then." Before (now)) p ("time. Date (). After (now): "and then. After (now) p ("Time"). Date (). Equal (now): "and then."  Equal (now)) p ("==================================")//The "Sub" Methods returns a "Duration" representing//the    Interval between. diff: = now. Sub (then) p ("time. Now (). Sub (time. Date ()): ", diff) p (" ================================== ")//We can compute the length of the duration in//Vari    ous units. P ("time. Date (). Sub (then). Hours (): ", diff. Hours ()) p ("time. Date (). Sub (then). Minutes (): ", diff. Minutes ()) p ("time. Date (). Sub (then). Seconds (): ", diff. Seconds ()) p ("time. Date (). Sub (then). nanoseconds (): ", diff. nanoseconds ()) p ("==================================")//can use ' Add ' to advance a time by a given// Duration, or with a "-" to move backwards by a//duration. P ("time. Date (). Sub (now). ADD (now. Sub): ", now. Add (diff)) p ("time. Date (). Sub (then). ADD (-(now). SUB)): ", then. ADD (-diff))}
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.