Get the start of the day for the given time
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
t := time .Now ()
fmt .Println (utils .StartOfDay (t ))
}
2024-12-29 00:00:00 +0500 PKT
Get the end of the day for the given time
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
t := time .Now ()
fmt .Println (utils .EndOfDay (t ))
}
2024-12-29 23:59:59.999999999 +0500 PKT
Add business days to a date (skipping weekends)
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
t := time .Date (2024 , 12 , 27 , 0 , 0 , 0 , 0 , time .Local ) // Friday
// Add 3 business days
result := utils .AddBusinessDays (t , 3 )
fmt .Println (result )
}
2025-01-01 00:00:00 +0500 PKT
Check if a given date is a weekend
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
saturday := time .Date (2024 , 12 , 28 , 0 , 0 , 0 , 0 , time .Local )
monday := time .Date (2024 , 12 , 30 , 0 , 0 , 0 , 0 , time .Local )
fmt .Printf ("Is Saturday a weekend? %v\n " , utils .IsWeekend (saturday ))
fmt .Printf ("Is Monday a weekend? %v\n " , utils .IsWeekend (monday ))
}
Is Saturday a weekend? true
Is Monday a weekend? false
5. TimeDifferenceHumanReadable
Get human-readable time difference
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
now := time .Now ()
future := now .Add (72 * time .Hour )
past := now .Add (- 48 * time .Hour )
fmt .Println (utils .TimeDifferenceHumanReadable (now , future ))
fmt .Println (utils .TimeDifferenceHumanReadable (now , past ))
}
Calculate duration until next specified weekday
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
now := time .Now ()
nextMonday := utils .DurationUntilNext (time .Monday , now )
fmt .Printf ("Duration until next Monday: %v\n " , nextMonday )
}
Duration until next Monday: 24h0m0s
Convert time to different timezone
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
t := time .Now ()
nyTime , err := utils .ConvertToTimeZone (t , "America/New_York" )
if err != nil {
fmt .Println ("Error:" , err )
return
}
fmt .Println (nyTime )
}
2024-12-29 14:00:00 -0500 EST
Format duration in human-readable format
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
d := 3 * time .Hour + 25 * time .Minute + 45 * time .Second
fmt .Println (utils .HumanReadableDuration (d ))
}
Calculate age from birthdate
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
birthDate := time .Date (1990 , 5 , 15 , 0 , 0 , 0 , 0 , time .Local )
age := utils .CalculateAge (birthDate )
fmt .Printf ("Age: %d years\n " , age )
}
Check if a year is a leap year
package main
import (
"fmt"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
fmt .Printf ("Is 2024 a leap year? %v\n " , utils .IsLeapYear (2024 ))
fmt .Printf ("Is 2023 a leap year? %v\n " , utils .IsLeapYear (2023 ))
}
Is 2024 a leap year? true
Is 2023 a leap year? false
Find next occurrence of a specific time
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
now := time .Now ()
nextNoon := utils .NextOccurrence (12 , 0 , 0 , now )
fmt .Println ("Next noon:" , nextNoon )
}
Next noon: 2024-12-30 12:00:00 +0500 PKT
Get ISO year and week number
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
t := time .Now ()
year , week := utils .WeekNumber (t )
fmt .Printf ("Year: %d, Week: %d\n " , year , week )
}
Calculate days between two dates
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
start := time .Date (2024 , 1 , 1 , 0 , 0 , 0 , 0 , time .Local )
end := time .Date (2024 , 12 , 31 , 0 , 0 , 0 , 0 , time .Local )
days := utils .DaysBetween (start , end )
fmt .Printf ("Days between: %d\n " , days )
}
Check if time is between two other times
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
now := time .Now ()
start := now .Add (- 1 * time .Hour )
end := now .Add (1 * time .Hour )
fmt .Printf ("Is current time between? %v\n " , utils .IsTimeBetween (now , start , end ))
}
Is current time between? true
Convert Unix milliseconds to time
package main
import (
"fmt"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
ms := int64 (1703836800000 ) // 2024-12-29 00:00:00
t := utils .UnixMilliToTime (ms )
fmt .Println (t )
}
2024-12-29 00:00:00 +0000 UTC
Split duration into components
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
d := 50 * time .Hour + 30 * time .Minute + 15 * time .Second
days , hours , minutes , seconds := utils .SplitDuration (d )
fmt .Printf ("Days: %d, Hours: %d, Minutes: %d, Seconds: %d\n " ,
days , hours , minutes , seconds )
}
Days: 2, Hours: 2, Minutes: 30, Seconds: 15
Get month name from number
package main
import (
"fmt"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
monthName , err := utils .GetMonthName (12 )
if err != nil {
fmt .Println ("Error:" , err )
return
}
fmt .Printf ("Month 12 is: %s\n " , monthName )
}
package main
import (
"fmt"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
dayName , err := utils .GetDayName (1 )
if err != nil {
fmt .Println ("Error:" , err )
return
}
fmt .Printf ("Day 1 is: %s\n " , dayName )
}
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
t := time .Now ()
formatted := utils .FormatForDisplay (t )
fmt .Println (formatted )
}
package main
import (
"fmt"
"time"
utils "github.com/kashifkhan0771/utils/time"
)
func main () {
now := time .Now ()
tomorrow := now .AddDate (0 , 0 , 1 )
fmt .Printf ("Is now today? %v\n " , utils .IsToday (now ))
fmt .Printf ("Is tomorrow today? %v\n " , utils .IsToday (tomorrow ))
}
Is now today? true
Is tomorrow today? false