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
+36.7k Upload multiple files with Go
+6.8k Golang : Use modern ciphers only in secure connection
+14.8k nginx: [emerg] unknown directive "ssl"
+30.4k error: trying to remove "yum", which is protected
+18.6k Golang : Calculate entire request body length during run time
+33.1k Golang : All update packages with go get command
+10.9k Golang : Post data with url.Values{}
+32.1k Golang : Regular Expression for alphanumeric and underscore
+11.9k Golang : Validate email address
+4k Javascript : How to show different content with noscript?
+29.9k Golang : Remove characters from string example