Golang : How to check if a date is within certain range?



Tags : golang date time within-range time-before time-after

Problem:

You are processing a large number of raw data files with dates as timestamps. However, you are only interested in processing the files within a certain range of a given date. For example, you want to process files that are two months before and after a given date. How to check if the dates on the files are within the 4 months range?

Solution:

This solution assumes that you've managed to extract(such as using awk) and isolate the dates from the filenames. It is a fairly simple process....get today's date, figure out the dates 2 months ago and 2 months later. (You can change the 2 months to any numbers you like)

The function below utilizes the time package Before() and After() function to check if the dates are within the 4 months range.

Here you go!


 package main

 import (
 "fmt"
 "time"
 )

 func DateWithinRange(dateString string, dateFormat string) (bool, error) {

 dateStamp, err := time.Parse(dateFormat, dateString)

 if err != nil {
 return false, err
 }

 today := time.Now()

 twoMonthsAgo := today.AddDate(0, -2, 0)  // minus 2 months
 twoMonthsLater := today.AddDate(0, 2, 0) // plus 2 months

 fmt.Println("Given : ", dateStamp.Format("02/01/2006"))
 fmt.Println("2 months ago : ", twoMonthsAgo.Format("02/01/2006"))
 fmt.Println("2 months later : ", twoMonthsLater.Format("02/01/2006"))

 if dateStamp.Before(twoMonthsLater) && dateStamp.After(twoMonthsAgo) {
 return true, nil
 } else {
 return false, nil
 }

 // default
 return false, nil
 }

 func main() {

 // dd/mm/yyyy
 result, err := DateWithinRange("27/04/2017", "02/01/2006")

 if err != nil {
 fmt.Println("Error :", err)
 }

 // %t to print boolean value in fmt.Printf
 fmt.Printf("Given date with range : [%t]\n", result)

 // yyyy-dd-mm
 result1, err := DateWithinRange("2017-27-04", "2006-02-01")

 if err != nil {
 fmt.Println("Error :", err)
 }

 fmt.Printf("Given date with range : [%t]\n", result1)

 // out of range example
 result2, err := DateWithinRange("2011-27-04", "2006-02-01")

 if err != nil {
 fmt.Println("Error :", err)
 }

 fmt.Printf("Given date with range : [%t]\n", result2)

 // out of range example
 result3, err := DateWithinRange("2018-17-09", "2006-02-01")

 if err != nil {
 fmt.Println("Error :", err)
 }

 fmt.Printf("Given date with range : [%t]\n", result3)
 }

Output:

Given : 27/04/2017

2 months ago : 27/02/2017

2 months later : 27/06/2017

Given date with range : [true]

Given : 27/04/2017

2 months ago : 27/02/2017

2 months later : 27/06/2017

Given date with range : [true]

Given : 27/04/2011

2 months ago : 27/02/2017

2 months later : 27/06/2017

Given date with range : [false]

Given : 17/09/2018

2 months ago : 27/02/2017

2 months later : 27/06/2017

Given date with range : [false]

References:

https://www.socketloop.com/tutorials/golang-change-date-format-to-yyyy-mm-dd

https://golang.org/pkg/time/#Time.Before

https://golang.org/pkg/time/#Time.After

https://www.socketloop.com/tutorials/golang-integer-is-between-a-range

Affiliate :

Hire Golang Developers


  See also : Golang : Integer is between a range



Tags : golang date time within-range time-before time-after

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement