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
+10.5k Golang : Find and draw contours with OpenCV example
+11.1k Golang : Add ASCII art to command line application launching process
+22.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+5.4k Golang : Convert an executable file into []byte example
+5k Golang : Extract unicode string from another unicode string example
+6.3k Golang : Calculate how many weeks left to go in a given year
+4.9k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+22.8k Golang : Daemonizing a simple web server process example
+4.5k Golang : fmt.Println prints out empty data from struct
+23.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+13.2k Golang : GUI with Qt and OpenCV to capture image from camera