<\/span><\/h1>\n\n\n\nTime<\/strong> or Date<\/strong> is represented in Go using time.Time<\/strong> struct. time can be also be represented as a<\/p>\n\n\n\nUnix Time (Also known as Epoch Time)<\/strong> – It is the number of seconds elapsed since 00:00:00 UTC on 1 January 1970. This time is also known as the Unix epoch.<\/li><\/ul>\n\n\n\n<\/p>\n\n\n\n
<\/span>Structure<\/strong><\/span><\/h1>\n\n\n\ntime.Time<\/strong> object is used to represent a specific point in time. The time.Time<\/strong> struct is as below<\/p>\n\n\n\ntype Time struct {\n \/\/ wall and ext encode the wall time seconds, wall time nanoseconds,\n \/\/ and optional monotonic clock reading in nanoseconds.\n wall uint64\n ext int64\n \/\/Location to represent timeZone\n \/\/ The nil location means UTC\n loc *Location\n}<\/code><\/pre>\n\n\n\nAs you can notice that every time.Time<\/strong> object has an associated location<\/strong> value which is used to determine the minute, hour, month, day and year corresponding to that time.<\/p>\n\n\n\n<\/p>\n\n\n\n
<\/span>Create a new time<\/strong><\/span><\/h1>\n\n\n\n<\/span>Using time.Now()<\/strong><\/span><\/h2>\n\n\n\nThis function can be used to get the current local timestamp. The signature of the function is<\/p>\n\n\n\n
func Now() Time<\/code><\/pre>\n\n\n\n<\/span>Using time.Date()<\/strong><\/span><\/h2>\n\n\n\nThis function returns the time which is yyyy-mm-dd hh:mm:ss + nsec<\/strong> nanoseconds with the appropriate time zone corresponding to the given location. The signature of the function is:<\/p>\n\n\n\nfunc Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Understanding Duration<\/strong><\/span><\/h1>\n\n\n\nduration<\/strong> is the time that has elapsed between two instants of time. It is represented as int64nanosecond<\/strong> count. So duration is nothing in Go but just a number representing time in nanoseconds. So if duration value is equal to 1000000000 <\/strong>then it represents 1 sec<\/strong> or 1000 milliseconds<\/strong> or 10000000000 nanoseconds<\/strong> <\/p>\n\n\n\nAs an example duration between two time values 1 hour apart will be below value which is equal number of nanoseconds in 1 hour. <\/p>\n\n\n\n
1 *60*60*1000*1000*1000<\/code><\/pre>\n\n\n\nIt is represented as below in the time<\/strong> package. <\/p>\n\n\n\ntype Duration int64<\/code><\/pre>\n\n\n\nBelow are some common duration which are defined in time<\/strong> package<\/p>\n\n\n\nconst (\n Nanosecond Duration = 1\n Microsecond = 1000 * Nanosecond\n Millisecond = 1000 * Microsecond\n Second = 1000 * Millisecond\n Minute = 60 * Second\n Hour = 60 * Minute\n)<\/code><\/pre>\n\n\n\nSome of the function defined on time.Time<\/strong> object that returns the Duration<\/strong> are<\/p>\n\n\n\nfunc (t Time) Sub(u Time) Duration<\/strong> – It returns the duration t-u <\/li>func Since(t Time) Duration – <\/strong>It returns the duration which has elapsed since t<\/li>func Until(t Time) Duration<\/strong> – It returns the duration until t<\/li><\/ul>\n\n\n\n<\/p>\n\n\n\n
<\/span>Add or Subtract to a time<\/strong><\/span><\/h1>\n\n\n\nNow that you have understood what duration , let’s see how we can add or subtract to a time instance<\/p>\n\n\n\n
time<\/strong> package in golang defines two ways of adding or subtracting to a time.<\/p>\n\n\n\nAdd <\/strong>function – It is used to add\/subtract a duration to time t. Since duration can be represented in hours, minutes, seconds, milliseconds, microseconds and nanoseconds, therefore Add function can be used to add\/subtract hours, minutes, seconds, milliseconds, microseconds and nanoseconds from a time . Its signature is<\/li><\/ul>\n\n\n\nfunc (t Time) Add(d Duration) Time<\/code><\/pre>\n\n\n\nAddDate <\/strong>function – It is used to add\/subtract years, months and days to time t. Its signature is <\/li><\/ul>\n\n\n\nfunc (t Time) AddDate(years int, months int, days int) Time<\/code><\/pre>\n\n\n\nNote: Positive values are used to add to time and negative values are used to subtract. Let’s see a working example of Add and Subtract to time.<\/p>\n\n\n\n
<\/span>Add to time <\/strong><\/span><\/h2>\n\n\n\nBelow code can be used to add to time<\/p>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"time\"\n)\n\nfunc main() {\n t := time.Now()\n\n \/\/Add 1 hours\n newT := t.Add(time.Hour * 1)\n fmt.Printf(\"Adding 1 hour\\n: %s\\n\", newT)\n\n \/\/Add 15 min\n newT = t.Add(time.Minute * 15)\n fmt.Printf(\"Adding 15 minute\\n: %s\\n\", newT)\n\n \/\/Add 10 sec\n newT = t.Add(time.Second * 10)\n fmt.Printf(\"Adding 10 sec\\n: %s\\n\", newT)\n\n \/\/Add 100 millisecond\n newT = t.Add(time.Millisecond * 10)\n fmt.Printf(\"Adding 100 millisecond\\n: %s\\n\", newT)\n\n \/\/Add 1000 microsecond\n newT = t.Add(time.Millisecond * 10)\n fmt.Printf(\"Adding 1000 microsecond\\n: %s\\n\", newT)\n\n \/\/Add 10000 nanosecond\n newT = t.Add(time.Nanosecond * 10000)\n fmt.Printf(\"Adding 1000 nanosecond\\n: %s\\n\", newT)\n\n \/\/Add 1 year 2 month 4 day\n newT = t.AddDate(1, 2, 4)\n fmt.Printf(\"Adding 1 year 2 month 4 day\\n: %s\\n\", newT)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nAdding 1 hour:\n 2020-02-01 02:16:35.893847 +0530 IST m=+3600.000239893\n\nAdding 15 minute:\n 2020-02-01 01:31:35.893847 +0530 IST m=+900.000239893\n\nAdding 10 sec:\n 2020-02-01 01:16:45.893847 +0530 IST m=+10.000239893\n\nAdding 100 millisecond:\n 2020-02-01 01:16:35.903847 +0530 IST m=+0.010239893\n\nAdding 1000 microsecond:\n 2020-02-01 01:16:35.903847 +0530 IST m=+0.010239893\n\nAdding 1000 nanosecond:\n 2020-02-01 01:16:35.893857 +0530 IST m=+0.000249893\n\nAdding 1 year 2 month 4 day:\n 2021-04-05 01:16:35.893847 +0530 IST<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Subtract to time <\/strong><\/span><\/h2>\n\n\n\nBelow code can be used to subtract to time<\/p>\n\n\n\n
package main\n\nimport (\n \"fmt\"\n \"time\"\n)\n\nfunc main() {\n t := time.Now()\n\n \/\/Add 1 hours\n newT := t.Add(-time.Hour * 1)\n fmt.Printf(\"Subtracting 1 hour:\\n %s\\n\", newT)\n\n \/\/Add 15 min\n newT = t.Add(-time.Minute * 15)\n fmt.Printf(\"Subtracting 15 minute:\\n %s\\n\", newT)\n\n \/\/Add 10 sec\n newT = t.Add(-time.Second * 10)\n fmt.Printf(\"Subtracting 10 sec:\\n %s\\n\", newT)\n\n \/\/Add 100 millisecond\n newT = t.Add(-time.Millisecond * 10)\n fmt.Printf(\"Subtracting 100 millisecond:\\n %s\\n\", newT)\n\n \/\/Add 1000 microsecond\n newT = t.Add(-time.Millisecond * 10)\n fmt.Printf(\"Subtracting 1000 microsecond:\\n %s\\n\", newT)\n\n \/\/Add 10000 nanosecond\n newT = t.Add(-time.Nanosecond * 10000)\n fmt.Printf(\"Subtracting 1000 nanosecond:\\n %s\\n\", newT)\n\n \/\/Add 1 year 2 month 4 day\n newT = t.AddDate(-1, -2, -4)\n fmt.Printf(\"Subtracting 1 year 2 month 4 day:\\n %s\\n\", newT)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nSubtracting 1 hour:\n 2020-02-01 00:18:29.772673 +0530 IST m=-3599.999784391\n\nSubtracting 15 minute:\n 2020-02-01 01:03:29.772673 +0530 IST m=-899.999784391\n\nSubtracting 10 sec:\n 2020-02-01 01:18:19.772673 +0530 IST m=-9.999784391\n\nSubtracting 100 millisecond:\n 2020-02-01 01:18:29.762673 +0530 IST m=-0.009784391\n\nSubtracting 1000 microsecond:\n 2020-02-01 01:18:29.762673 +0530 IST m=-0.009784391\n\nSubtracting 1000 nanosecond:\n 2020-02-01 01:18:29.772663 +0530 IST m=+0.000205609\n\nSubtracting 1 year 2 month 4 day:\n 2018-11-27 01:18:29.772673 +0530 IST<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Time Parsing\/Formatting<\/strong><\/span><\/h1>\n\n\n\n If you have worked with time\/date formatting\/parsing in other languages you might have noticed that the other languages use special placeholders for time\/date formatting. For eg ruby language uses<\/p>\n\n\n\n
%d for day<\/li> %Y for year<\/li><\/ul>\n\n\n\netc<\/p>\n\n\n\n
Golang, instead of using codes such as above, uses date and time format placeholders that look like date and time only. Go uses standard time, which is:<\/p>\n\n\n\n
Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)\nor \n01\/02 03:04:05PM '06 -0700<\/code><\/pre>\n\n\n\n So if you notice go uses <\/p>\n\n\n\n
01 for day of the month , <\/li> 02 for the month <\/li> 03 for hours , <\/li> 04 for minutes <\/li> 05 for second <\/li> and so on <\/li><\/ul>\n\n\n\nBelow placeholder table describes the exact mapping. Go takes a more pragmatic approach where you don’t need to remember or lookup for the traditional formatting codes as in other languages<\/p>\n\n\n\nType<\/strong><\/td>Placeholder<\/strong><\/td><\/tr>Day<\/td> 2<\/strong> or 02<\/strong> or _2<\/strong><\/td><\/tr>Day of Week<\/td> Monday<\/strong> or Mon<\/strong><\/td><\/tr>Month<\/td> 01<\/strong> or 1<\/strong> or Jan<\/strong> or January<\/strong><\/td><\/tr>Year<\/td> 2006<\/strong> or 06<\/strong><\/td><\/tr>Hour<\/td> 03<\/strong> or 3 <\/strong>or 15<\/strong><\/td><\/tr>Minutes<\/td> 04<\/strong> or 4<\/strong><\/td><\/tr>Seconds<\/td> 05<\/strong> or 5<\/strong><\/td><\/tr>Milli Seconds (ms)<\/td> .000 <\/strong>\/\/Trailing zero will be includedor .999 <\/strong> \/\/Trailing zero will be omitted<\/td><\/tr>Micro Seconds (\u03bcs)<\/td> .000000 <\/strong>\/\/Trailing zero will be includedor .999999<\/strong> \/\/Trailing zero will be omitted<\/td><\/tr>Nano Seconds (ns)<\/td> .000000000 <\/strong>\/\/Trailing zero will be includedor .999999999 <\/strong> \/\/Trailing zero will be omitted<\/td><\/tr>am\/pm<\/td> PM<\/strong> or pm<\/strong><\/td><\/tr>Timezone<\/td> MST<\/strong><\/td><\/tr>Timezone offset<\/td> Z0700 <\/strong>or Z070000<\/strong> or Z07<\/strong> or Z07:00<\/strong> or Z07:00:00<\/strong> or -0700<\/strong> or -070000<\/strong> or -07<\/strong> or -07:00<\/strong> or -07:00:00<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/span>Time Parse Example<\/strong><\/span><\/h2>\n\n\n\nNow coming back to time.Parse<\/strong>. The signature of the function is<\/p>\n\n\n\nfunc Parse(layout, value string) (Time, error)<\/code><\/pre>\n\n\n\ntime.Parse<\/strong> function takes in two arguments<\/p>\n\n\n\nFirst argument is the layout consisting of time format placeholder<\/li><\/ul>\n\n\n\n