On Friday, January 24, 2020 at 12:20:26 PM UTC-5, Constantine Vassilev 
wrote:
>
> I've the start-end date, then how to do that?
>

Look at the time package (https://golang.org/pkg/time/). In particular, 
time.Date() https://golang.org/pkg/time/#Date is your friend for making a 
time.Time for any date. 

Also "4d63.com/tz" package (https://github.com/leighmcculloch/go-tz) is 
useful for being portable to windows, where Go's timezones are 
missing/broken by default (https://github.com/golang/go/issues/21881 ). 

You don't have to use UTC if you know the time.Location in the following 
example. 

import "time"

// Date represents a UTC time zone day
type Date struct {
    Year  int 
    Month int 
    Day   int 
}

// ToGoTime turns the date into UTC time.Time, at the 0 hrs 0 min 0 second 
start of the day.
func (d *Date) ToGoTime() time.Time {
    return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, 
time.UTC)
}

func DaysBetweenAminusB(a, b *Date) int {
    return int(int64(a.ToGoTime().Sub(b.ToGoTime())) / int64(time.Hour*24))
} 

Display of a time.Time is a simple matter of choosing one of the formats in 
time (see the Constants section at the top), and using Format() 
https://golang.org/pkg/time/#Time.Format

e.g.

import "4d63.com/tz"
import "fmt"
import "time"

var d Date
var NYC *time.Location
NYC, err = tz.LoadLocation("America/New_York")
if err != nil {panic(err)}

hr := 12 // noon
min := 0
sec := 0
msec := 0

tm := time.Date(d.Year, time.Month(d.Month), d.Day, hr, min, sec, msec*1e6, 
NYC)

fmt.Printf("%v", tm.In(NYC).Format(time.RFC3339Nano))

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a22fe606-a63a-4f95-bc4a-1327dfec99e2%40googlegroups.com.

Reply via email to