Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert date to the string in go using locale?

Tags:

date

go

locale

I convert date to a string this way:

d.Format("Mon 02. Jan")

and I get something like

Fri 27. Jan

How can I switch the locale and get the string in other language?

like image 426
Igor Chubin Avatar asked Jan 27 '17 15:01

Igor Chubin


People also ask

How to parse a date in Go language?

To parse the date in Go, we use something called layout. There is a standard time in Go which we need to remember while parsing dates in Go: It is January 2nd, 3:04:05 PM of 2006, UTC-0700. To parse a date string, we pass this layout to the time.Parse function.

How to convert a datetime to a string in C++?

To convert a datetime to a string, you use the CONVERT () function as follows: VARCHAR is the first argument that represents the string type. datetime is an expression that evaluates to date or datetime value that you want to convert to a string. sytle specifies the format of the date.

How to get the current date and time as a string?

The date can be a literal or an expression that evaluates to a DATE value. The string can be any character string data type such as VARCHAR or TEXT. The CAST () function returns a string that represents the date. The following statement returns the current date and time as a date and as a string:

What is the date format in go?

Go uses a standard date, unlike other programming languages that use codes to represent parts of a date/time string representation. Parsing in Go is done with the help of Parse and Format functions in the time package.


1 Answers

You can't. The Go standard library does not contain localized month, day and zone names. The names are wired into the time package.

For example, the name of the months returned by Month.String() are stored in the unexported time.month global variable:

var months = [...]string{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
}

func (m Month) String() string { return months[m-1] }

Similarly, names of weekdays come from Weekday.String(), stored in unexported variable time.days.

Having said that, there may be 3rd party libraries supporting your needs. Here's an incomplete one which might be of some help to you: https://github.com/mattbaird/go-i18n-formats

As shared by Igor Chubin below, this 3rd party lib is much more complete: https://github.com/klauspost/lctime

Also note that while providing a general, multilingual time formatting package is not an easy task, if you really need it, you could take the time package, copy it to your project and just translate the names to the language you need.

Also note that supporting a low number of languages and low number of layouts, it's easy to create the formatting yourself.

For example, the code below formats a given time.Time value in Hungarian, using the layout you used in your question:

func Format(t time.Time) string {
    return fmt.Sprintf("%s %02d. %s",
        days[t.Weekday()][:3], t.Day(), months[t.Month()-1][:3],
    )
}

var days = [...]string{
    "Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"}

var months = [...]string{
    "Január", "Február", "Március", "Április", "Május", "Június",
    "Július", "Augusztus", "Szeptember", "Október", "November", "December",
}

Testing it:

fmt.Println(Format(time.Now()))

Output on the Go Playground:

Ked 10. Nov

Output on my local machine:

Pén 27. Jan
like image 97
icza Avatar answered Sep 18 '22 19:09

icza