From the course: Learning Go

Work with dates and times - Go Tutorial

From the course: Learning Go

Work with dates and times

- [Instructor] Dates and times are managed in Go with the Time package. A variable declared with the time type encapsulates everything you need for both dates and times, including math operations, time zone management, and so on. To demonstrate this I'll add the time package to my import block and then down here in the main function, I'll create a variable that I'll just call n, for now, and I'll get that value from time.Now a function that's a member of that package. And then I'll output the string, I recorded this video at and I'll pass in n. And when I run the code, I get a long string, a timestamp, that includes the current date, time, and time zone. Now I can also create my own explicit date/time values. This time I'll create a variable called t and I'll use time.Date. This function requires a year, a month, a day, an hour, a minute, a second, and a nanosecond, and something called a location. I'll pass in a year of 2009. For the month, I'll use a constant of the time package, November, and then a date of 10, an hour value of 23, we're in a 24-hour clock, zeros for the minutes, seconds, and milliseconds, and then finally for the location, I'll pass in the constant of time.UTC for Universal Time. And then I'll output that with Println and I'll output, Go launched at and then I'll pass in the value of t. And I see that the Go language project launched on November 10th, 2009, at 11:00 PM UTC. I can also output a formatted version of the date/time value. To do that, once again, I'll use Println and I'll output t.Format. This function, and many others, are available from the time object itself. Next, I need a layout string which is a representation of the formatted time value. There are a bunch of constants available for this and I'll use time.ANSIC. And now the output is much cleaner and easier to read and includes the day of the week. Check the documentation for other constants for other standard formats. Now, if you want to take that in the other direction you could select this string and copy it to the clipboard. And then I'll create a variable called parsedTime and I'll get its value from a function called time.Parse. Once again, I need a layout and I'll use the same layout as before, ANSIC, and I'll paste in that string value wrapped in quotes. And to prove that I got back at time object, I'll use Printf this time and I'll say, The type of parsedTime is %T uppercase. I'll make sure to include my line feed. And then I'll pass in parsedTime. I'll correct my spelling over here. And notice when I saved my changes that there was a syntax error, and that's because the parse function can return an error object. I want to ignore that error object so I'll add comma underscore. The error goes away when I save. I'll run it the code. And I see that the type of the parsedTime object is time.time. The time object has many other useful functions. For example, you can add dates together and you can do special kinds of formatting. There are patterns available that give you complete flexibility in how to format date/time values. The functions and constants in the time package give you all the tools you need to store and manage date/time values. For more in this topic check out the documentation on the time package in the Golang developer website.

Contents