Golang : How to check if a date is within certain range?
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
See also : Golang : Integer is between a range
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
Tutorials
+14.6k Golang : How to reverse elements order in map ?
+8.7k Golang : List available AWS regions
+6k Golang : A simple forex opportunities scanner
+6.8k Swift : Convert (cast) String to Double
+7.2k Your page has meta tags in the body instead of the head
+12.9k Golang : Convert IP version 6 address to integer or decimal number
+17.8k Golang : Fix cannot download, $GOPATH not set error
+18.5k Golang : Append content to a file
+3.8k Linux/MacOSX : How to symlink a file?
+9.5k Golang : Roll the dice example
+6.9k Golang Hello World Example
+19.5k Golang : Clean up null characters from input data