Skip to content

Instantly share code, notes, and snippets.

@febriliankr
Created July 11, 2023 10:42
Show Gist options
  • Save febriliankr/6a576644ed9d4fd12233d67d69a6544d to your computer and use it in GitHub Desktop.
Save febriliankr/6a576644ed9d4fd12233d67d69a6544d to your computer and use it in GitHub Desktop.
Days Before Date
package main
import (
"fmt"
"time"
)
func daysFromNow(targetTime time.Time) int {
// Convert to capture date only, without time
year, month, day := time.Now().Date()
reducedNow := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
targetYear, targetMonth, targetDay := targetTime.Date()
reducedTargetTime := time.Date(targetYear, targetMonth, targetDay, 0, 0, 0, 0, time.UTC)
duration := reducedTargetTime.Sub(reducedNow)
daysDifference := int(duration.Hours() / 24)
return daysDifference
}
func main() {
targetDate := time.Date(2023, time.July, 15, 23, 0, 0, 0, time.UTC)
difference := daysFromNow(targetDate)
fmt.Println(fmt.Sprintf("D-%v", difference))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment