Define date type inside struct

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
}

func main() {
s1, _:= time.Parse(time.RFC3339, “2018-12-12T11:45:26.371Z”)
s := person{name: “Sean”, age: 50, DOB:s1}
fmt.Println(s)
}

You have Date() method on the time.Time type.

https://golang.org/pkg/time/#Time.Date

Do you want this just for printout or something else? If yes, you can just implement Stringer interface on your struct.

https://golang.org/pkg/fmt/#Stringer

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

Just parse that input date. How exactly do you get it? What format? Can you show some input data example?

This is what i am getting. that is blank date when i pass it. i get all zero

package main

import( “fmt”
“time”)

// This person struct type has name,age and datfields.
type person struct {
name string
age int
DOB time.Time
}

func main() {
s1, _:= time.Parse(time.RFC3339, “2018-12-12”)
s := person{name: “Sean”, age: 50, DOB:s1}
fmt.Println(s)
}

Output:-
{Sean 50 0001-01-01 00:00:00 +0000 UTC}

As you can see from the documentation, time.RFC3339 requires full date time.

For me it seems as if using "2006-01-02" as first argument is sufficient:

https://play.golang.org/p/7iKJPKxhPRT

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?

Then you’ll need to implement your own Date type. But to be honest, you really do not want to do that.

It is common practice to simply use a DateTime type and ignore the fractions of the day.

Can you please suggest how do i ignore that fractions of the day 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.

Example:

func (p person) String() string {
    return fmt.Sprintf("{%s %d %s}", p.name, p.age, p.DOB.Format("2006-01-02"))
}
2 Likes

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

True, i didn’t really understand the reason why it specifically look for the date 2006 01 02. When i changed the year to 2018 then it throws an error.

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.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.