Golang : Get today's weekday name and calculate target day distance example
Let's calculate how long it will take to reach Friday from today using switch evaluation. The code sample below will get today's weekday and evaluate the sum of today + n (n is the number of days) before hitting Friday.
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now().Weekday()
fmt.Println("Today is ", today)
fmt.Println("When's Friday?")
switch time.Friday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
case today + 3:
fmt.Println("In three days.")
case today + 4:
fmt.Println("In four days.")
case today + 5:
fmt.Println("In five days.")
default:
fmt.Println("Will be sometime away.")
}
}
Sample output :
Today is Monday
When's Friday?
In four days.
Reference :
https://www.socketloop.com/references/golang-time-time-weekday-function-example
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
+11.4k SSL : The certificate is not trusted because no issuer chain was provided
+9k Golang : How to get username from email address
+13.2k Golang : Increment string example
+10k Golang : Detect number of faces or vehicles in a photo
+21.8k Golang : Match strings by wildcard patterns with filepath.Match() function
+12.4k Golang : Remove or trim extra comma from CSV
+26.5k Golang : Find files by extension
+6.4k Golang : Check if password length meet the requirement
+5.9k Java : Human readable password generator
+16.5k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer
+11k Use systeminfo to find out installed Windows Hotfix(s) or updates