Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datatypes.Date to time.Time #72

Open
saul-data opened this issue Sep 23, 2021 · 2 comments
Open

datatypes.Date to time.Time #72

saul-data opened this issue Sep 23, 2021 · 2 comments
Assignees

Comments

@saul-data
Copy link

Your Question

In the documentation it talks about how to go from time.Time to datatypes.Date() - insert a time into database.

Reference:
https://github.com/go-gorm/datatypes/
https://github.com/go-gorm/datatypes/blob/master/date.go

But to retrieve a date from the database and convert to time.Time, I searched quite a lot and tried different methods to go from datatypes.Date() to time.Time.

I got this to work but I'm sure its probably not the best method.

x, _ :=  dbrecord.Date.MarshalJSON()
bDate, _ := time.Parse(time.RFC3339, strings.Trim(string(x), "\""))

The document you expected this should be explained

https://github.com/go-gorm/datatypes/

Expected answer

It would be good to have best practice in the readme on how to do this.

@seanballais
Copy link

seanballais commented Nov 9, 2021

I found another way of converting datatypes.Date to time.Time. You can do:

dVal, _ := dbRecord.Date.Value()
d := dVal.(time.Time)

EDIT: Only use this if you only care about the date part. The time gets removed.

@glebtv
Copy link

glebtv commented May 5, 2023

This seems to work:

t := time.Time(dbRecord.Date)

Go 1.20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants