Golang : Get month name from date example
Problem:
You have a date and you want to get the month name from the date. For example,10/10/2017
should give you October or 03/11/2018
returns March. Both dates are mm/dd/yyyy
format.
How to do that?
Solution:
Use the .Date()
method to get month name. You can convert the date to Golang's time.Time
type first if needed. See https://www.socketloop.com/tutorials/golang-convert-date-or-time-stamp-from-string-to-time-time-type
Here is a sample code:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Now is : ", now)
// get month name from now
_, month, _ := now.Date()
fmt.Println("and the month is : ", month)
}
Output:
Now is : 2017-02-07 11:51:46.190819028 +0800 MYT
and the month is : February
References:
https://www.socketloop.com/tutorials/golang-how-to-get-year-month-and-day
See also : Golang : How to get year, month and day?
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
+7.2k Golang : Check if integer is power of four example
+21.2k Golang : Round float to precision example
+11k Golang : List running EC2 instances and descriptions
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+10.8k Golang : Determine if time variables have same calendar day
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+5.9k Golang : How to call function inside template with template.FuncMap
+7.8k Golang : Find duplicate files with filepath.Walk
+7.2k Your page has meta tags in the body instead of the head
+4.8k Golang : Struct field tags and what is their purpose?
+7k Swift : Convert (cast) String to Float
+4.1k Swift : Convert (cast) Float to Int or Int32 value