The moment2go
package provides functionality to convert Moment.js date and time formats into Go date and time layouts. It offers a seamless way to integrate familiar Moment.js-style date formatting into Go applications, while maintaining thread-safety and extensibility.
- Converts Moment.js format tokens to Go layout tokens.
- Thread-safe and efficient conversion for concurrent applications.
- Supports location-based date and time formatting.
To use this package, install it with:
go get github.com/axkit/moment2go
Format a time.Time
value using a Moment.js format:
package main
import (
"fmt"
"time"
"github.com/axkit/moment2go"
)
func main() {
converter := moment2go.New()
t := time.Date(2023, 1, 1, 15, 30, 0, 0, time.UTC)
formattedTime := converter.Format("YYYY-MM-DD HH:mm", t)
fmt.Println("Formatted Time:", formattedTime)
}
Convert a Moment.js format string into a Go layout string:
package main
import (
"fmt"
"github.com/axkit/moment2go"
)
func main() {
momentFormat := "YYYY-MM-DD"
goFormat := moment2go.ConvertMomentFormat(momentFormat)
fmt.Println("Go Format:", goFormat)
}
Convert a Moment.js format string and apply a location-specific time zone offset:
package main
import (
"fmt"
"time"
"github.com/axkit/moment2go"
)
func main() {
location, _ := time.LoadLocation("America/New_York")
momentLayout := "YYYY-MM-DD"
goLayout := moment2go.ConvertMomentToGoLayoutWithLocation(momentLayout, location)
fmt.Println("Go Layout with Location:", goLayout)
}
Converts a Moment.js date and time format to a Go date and time format.
Converts a Moment.js date and time layout to a Go layout with a time zone offset.
Thread-safe converter for Moment.js formats.
New() *Moment2Go
: Creates a newMoment2Go
instance.Convert(momentLayout string) string
: Converts a Moment.js format string to a Go layout.Format(momentLayout string, t time.Time) string
: Formats atime.Time
value using a Moment.js layout.
Run the tests with:
go test ./...
Example test cases are included in the moment2go_test.go
file, covering:
- Basic format conversions.
- Complex format handling.
- Thread-safety validations.
Contributions are welcome! Please open an issue or submit a pull request for bug fixes, feature requests, or improvements.
This project is licensed under the MIT License. See the LICENSE
file for details.