Golang program to get the current weekday


In this Go language article, we will write proCurrent weekday implies the current day in the week which can be obtained using numerous methods. In this program we will use three examples to get the current weekday for which time package will be imported in the program, it helps in viewing the time.

Syntax

func Now() Time

The Now() function is defined in time package. this function generates the current local time. To use this function, we have to first import the time package in our program.

time.Format()

This function belongs to the time package. It is used to format the time on the basis of the layout string.

Using Now and Weekday Function

In this method, we will write a Golang program to get the current weekday by using the built-in functions named Now and Weekday. The weekday method will be called upon the time.Time and the desired value is obtained.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and time package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function and in that function create a variable current_day to store the current weekday value.

  • Step 3 − Use time.Now function from the time package to get the current time. Then, call the weekday function on that current time.Time to obtain the current weekday.

  • Step 4 − Print the current_weekday on the console using fmt.Println() function where ln means new line.

Example

The following example, Golang program to get the current weekday using Now and Weekday function

package main

import (
   "fmt"
   "time"  
)

//Main function to execute the program
func main() {
   current_day := time.Now().Weekday()  
   fmt.Println("Today is", current_day) 
}

Output

Today is Tuesday

Using Now and Format Function

In the following illustration, Now and format are used to get the current weekday value. The formatted string will be given as an input to Now.format function.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and time package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a function main and in this function create a variable named current_weekday to store the weekday value.

  • Step 3 − Then, use Now.format to get the string that represents formatted time and takes a formatted string as an argument. Here that string is “Monday”.

  • Step 4 − Print the Output on the console using fmt.Println() function where ln means new line.

Example

The following golang example illustrates Golang program to get the current weekday using Now and format function

package main

import (
   "fmt"
   "time"          
)

func main() {
   current_weekday := time.Now().Format("Monday")
   fmt.Println("Today is", current_weekday)
}

Output

Today is Tuesday

Using Switch Statement

This method executes similarly like example1, the only difference is here the weekday is printed using switch statement conditions where time.Day is kept as a condition obtained from the Now and Weekday function.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and time package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a function main and in this function use a current_weekday variable to store the current weekday value.

  • Step 3 − Perform the similar operation as done in example1 using Now and Weekday method.

  • Step 4 − Then, use the switch case statements to print the corresponding weekday using time.Day.

  • Step 5 − The desired day will be printed on the console with the statement "Today is Monday" or any other day as per the situation.

  • Step 6 − The print statement is executed using fmt.Println() function where ln means new line.

Example

The following example demonstrates Golang program to get the current weekday using switch statement

package main

import (
   "fmt"
   "time"   
)

func main() {
   current_weekday := time.Now().Weekday()  
   switch current_weekday {              
   case time.Monday:
      fmt.Println("Today is Monday")   
   case time.Tuesday:
      fmt.Println("Today is Tuesday")       
   case time.Wednesday:
      fmt.Println("Today is Wednesday")   
   case time.Thursday:
      fmt.Println("Today is Thursday")       
   case time.Friday:
      fmt.Println("Today is Friday")            
   case time.Saturday:
      fmt.Println("Today is Saturday")        
   case time.Sunday:
      fmt.Println("Today is Sunday")           
   }
}

Output

Today is Tuesday

Conclusion

We executed the program of getting the current weekday using three examples. In the first example we used Now and Weekday method, in the second example we used Now and format method to obtain the Output and in the third example we used switch cases. All the examples returned the desired Output.

Updated on: 03-Apr-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements