Skip to content

About

This package helps to format dates according to Thai standards and convert numbers to Thai text, making it suitable for applications that need to display data in Thai specifically.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

55 Commits

Folders and files

Repository files navigation

convert-thai-go ✨

convert-thai-go is a Go package for Thai date formatting, Thai numeral conversion, and Thai Baht text generation.

แพ็กเกจสำหรับจัดรูปแบบวันที่ภาษาไทย แปลงเลขอารบิกเป็นเลขไทย และแปลงจำนวนเงินเป็นข้อความภาษาไทย

Go Reference Go Report Card License


Features

  • Thai number formatting
  • Thai numeral conversion
  • Thai Baht text generation
  • Thai date formatting
  • Locale support
    • th Thai locale with Thai numerals
    • en English locale with Arabic numerals
  • Multiple era support
    • BE Buddhist Era
    • CE Common Era
    • MS Mahasakaraj / Saka Era
    • CS Chulasakaraj Era
    • RE Rattanakosin Era
    • HE Hijri Era (approximate)

Installation

go get github.com/SiamCore/convert-thai-go

Usage

package main

import (
	"fmt"
	"time"

	convertthai "github.com/SiamCore/convert-thai-go"
)

func main() {
	// Thai number formatting
	numTH := convertthai.NumberFormat(1000, &convertthai.NumberFormatOptions{
		Decimals:           2,
		UseThaiNumber:      true,
		ThousandsSeparator: true,
	})
	fmt.Println(numTH)
	// Output: ๑,๐๐๐.๐๐

	// English number formatting
	numEN := convertthai.NumberFormat(1000, &convertthai.NumberFormatOptions{
		Decimals:           2,
		UseThaiNumber:      false,
		ThousandsSeparator: true,
	})
	fmt.Println(numEN)
	// Output: 1,000.00

	// default Thai number formatting
	num := convertthai.NumberFormat(1000, nil)
	fmt.Println(num)
	// Output: ๑๐๐๐

	// Thai Baht text
	baht := convertthai.ThaiBath(1000)
	fmt.Println(baht)
	// Output: หนึ่งพันบาทถ้วน

	// Default Thai locale (BE)
	dateTH := convertthai.DateFormat(time.Now(), "dddd dd mmmm yy", nil)
	fmt.Println(dateTH)
	// Output: อังคาร ๐๓ พฤศจิกายน ๖๙

	// English locale (CE)
	locale := "en"
	dateEN := convertthai.DateFormat(time.Now(), "dddd dd mmmm yy", &convertthai.DateFormatOptions{Locale: locale})
	fmt.Println(dateEN)
	// Output: Tuesday 03 November 26

	// Custom era
	era := "HE"
	dateRE := convertthai.DateFormat(time.Now(), "dd/mm/yyyy", &convertthai.DateFormatOptions{Era: era})
	fmt.Println(dateRE)
	// Output: ๐๓/๑๑/๑๔๔๗

	thaiDate := convertthai.FormatThaiDate(time.Now(), "dddd dd mmmm yyyy")
	fmt.Println(thaiDate)
	// Output: อังคาร ๐๓ พฤศจิกายน ๒๕๖๙

	date := convertthai.FormatDate(time.Now(), "dddd dd mmmm yyyy")
	fmt.Println(date)
	// Output: Tuesday 03 November 2026

	dateStr := "2026-11-03"
	parsedDate, _ := convertthai.StringToDate(dateStr)

	dateFromStr := convertthai.FormatDate(parsedDate, "dddd dd mmmm yyyy")
	fmt.Println("dateFromStr: ", dateFromStr)
	// Output: Tuesday 03 November 2026

	thaiDateFromStr := convertthai.FormatThaiDate(parsedDate, "dddd dd mmmm yyyy")
	fmt.Println("thaiDateFromStr: ", thaiDateFromStr)
	// Output: อังคาร ๐๓ พฤศจิกายน ๒๕๖๙
}

Date Format Tokens

Token Description Example (th) Example (en)
d Day without leading zero ๑ 1
dd Day with leading zero ๐๑ 01
ddd Short weekday จ Mon
dddd Full weekday จันทร์ Monday
m Month without leading zero ๑ 1
mm Month with leading zero ๐๑ 01
mmm Short month ม.ค. Jan
mmmm Full month มกราคม January
yy Two-digit year ๖๙ 26
yyyy Full year ๒๕๖๙ 2026
H Hour without leading zero ๙ 9
HH Hour with leading zero ๐๙ 09
M Minute without leading zero ๕ 5
MM Minute with leading zero ๐๕ 05
S Second without leading zero ๘ 8
SS Second with leading zero ๐๘ 08

Format Examples

Format Example (th) Example (en)
dd/mm/yyyy ๒๐/๐๕/๒๕๖๙ 20/05/2026
dd/mm/yy ๒๐/๐๕/๖๙ 20/05/26
yyyy/mm/dd ๒๕๖๙/๐๕/๒๐ 2026/05/20
HH:MM:SS ๐๗:๓๐:๒๕ 07:30:25
dd mmm yyyy ๒๐ พ.ค. ๒๕๖๙ 20 May 2026
dd mmmm yyyy ๒๐ พฤษภาคม ๒๕๖๙ 20 May 2026
ddd dd mmm yyyy พ ๒๐ พ.ค. ๒๕๖๙ Wed 20 May 2026
dddd dd mmmm yyyy พุธ ๒๐ พฤษภาคม ๒๕๖๙ Wednesday 20 May 2026
dd/mm/yyyy HH:MM:SS ๒๐/๐๕/๒๕๖๙ ๐๗:๓๐:๒๕ 20/05/2026 07:30:25

Supported Locales

Locale Description
th Thai locale with Thai numerals
en English locale with Arabic numerals

Supported Eras

Era Description Example (th) Example (en)
BE Buddhist Era (พ.ศ.) ๒๕๖๙ 2569
CE Common Era / Christian Era (ค.ศ.) ๒๐๒๖ 2026
MS Mahasakaraj / Saka Era (มหาศักราช) ๑๙๔๘ 1948
CS Chulasakaraj Era (จุลศักราช) ๑๓๘๘ 1388
RE Rattanakosin Era (รัตนโกสินทรศก) ๒๔๕ 245
HE Hijri Era (ฮิจเราะห์ศักราช, approximate) ๑๔๔๗ 1447

Default Behavior

Locale Default Era
th BE
en CE

Example Output

อังคาร ๐๓ พฤศจิกายน ๒๕๖๙
Tuesday 03 November 2026
หนึ่งพันบาทถ้วน
๑,๐๐๐.๐๐
1,000.00

License

MIT License

About

This package helps to format dates according to Thai standards and convert numbers to Thai text, making it suitable for applications that need to display data in Thai specifically.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages