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
+12.8k Golang : Simple word wrap or line breaking example
+5.6k Golang : When to use make or new?
+13.4k Golang : Save(pipe) HTTP response into a file
+3.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+4.8k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+17.3k Golang : Example for RSA package functions
+10.4k SSL : The certificate is not trusted because no issuer chain was provided
+12.9k Golang : Get uploaded file name or access uploaded files
+25.1k Golang : Encrypt and decrypt data with AES crypto
+8.1k Golang : Create and shuffle deck of cards example
+5.1k Golang : Debug with Godebug
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)