HI everyone, i am trying to define Date type inside struct but i am unable to do it. i have done research but i couldnt realy find anything for date. i dont wan to take time with that.
when i run below code i get the date and time but i dont want to time because my struct is going to receive date only. is there any better way please?
package main
import( “fmt”
“time”)
// This person struct type has name,age and datfields.
type person struct {
name string
age int
DOB time.Time
}
I am receiving the date parameter from other program(interface) and I want that move in my struct and then I am using the that struct for other things like database update all. Please suggest
You are right but the issue with the example you given is those extra zeros. i want data only not the extending zeros. is there any way to fetch only date please?
That depends on what you actually want. If its just printing, use a proper format string. If its just parsing, the same. If it is comparing, well, normalize to a certain hour/minute/second and then use regular comparison methods.
after performing below line
s1, _ := time.Parse(format, “2018-12-12”)
i want to move that s1 into another struct for example
type person struct {
name string
age int
DOB time.Time
}
my question should be the DOB type time.Time? or something else? can you please suggest how this can be moved in person struct and what would be the DOB type.
time.Parse() returns (time.Time, error), so you either use that in your struct, or you use time.Parse() only for verification and store the plain string representation and only parse again if ever necessary. There might be considerations in place we do not know which may favor one version over the other.
If you want to get a string from a time.Time, the best way is to use the Format(string) string method.
For your purposes you would want to do s.DOB.Format("2006-01-02") to obtain a string of the format you wish.
If you want to have the date formatted properly when you print the struct itself, you will need to implement the String() string method on your struct, which would then call the Format method on the time.
I was doing the same thing in Hugo which is written in go. For some reason or another you have to use 2006 01 02 or it will give you a funky numbers in the year. Should work though using .Format
People tend to get confused with Go’s rather strange approach to date and time handling. Go’s time package doesn’t adhere to the common "dd/mm/yyyy hh:mm:ss" (with proper cases depending on the language) formatting strings. Instead, it defines a known date, Mon Jan 2 15:04:05 MST 2006 or 2016-01-02 15:04:05 in yyyy-mm-dd hh:mm:ss, from which to derive the format given a desired layout. So for example calling someDate.Format("02/01/2016 15:04:05") would format someDate as "dd/mm/yyyy hh:mm:ss" would do in other languages.
Analogously, dates are parsed from strings by passing the layout in which the string is formatted and the actual timestamp string value to the Parse function. So calling time.Parse("02/01/2016", "18/12/2018") would return the timestamp 2018-12-18 00:00:00 +0000 UTC (if printed in the yyyy-mm-dd hh:mm:ss format).
You can check everything at the docs, and here are some more examples.