Home > Back-end >  How to calculate between days?
How to calculate between days?

Time:03-26

i'm new in golang. Can u teach me how to calculate between two days. This is my code, i want to calculate betweet Time and Created At

type ActivityHistoryData struct {
    AgentId            uint
    ActionName         string
    Time               time.Time
    ActorId            uint32
    AcqFullname        string
    CreatedAt          time.Time
}

Thank you

I' tried it, and there is some error in my code. Especially when i added time with created at. The error is "type ActicvityHistoryData has no method Time" and "type ActicvityHistoryData has no method CreatedAt"

CodePudding user response:

It looks like somewhere in your code, in the part that you have not shared, you are adding () after the .Time and .CreatedAt fields. The expression x.f() (where x is not a package) represents a method call and assumes f is a method of x. Time and CreatedAt are not methods of ActivityHistoryData, they are fields and therefore you SHOULD NOT append () to them.

To calculate the number of days between two time.Time values you can do the following:

var t1 time.Time = ...
var t2 time.Time = ...

numberOfDays := t2.Sub(t1).Hours() / 24

https://go.dev/play/p/GDI8VUiYsZn

CodePudding user response:

If I understood correctly, you are trying to calculate difference between two times.

Here is an example: https://go.dev/play/p/h-dX4MqRf6p keep in maind that, The Go Playground's time.Now() allways returns 2009-11-10 23:00:00

  • Related