A minimal, portable time utility for Go and TinyGo with WebAssembly support. Automatically handles timezones and uses JavaScript Date APIs in WASM environments to keep binaries small.
import "github.com/tinywasm/time"
func main() {
// Get current Unix timestamp in nanoseconds (UTC)
nano := time.Now()
println("Current time:", nano)
// Format dates and times (Using detected local timezone)
date := time.FormatDate(nano)
println("Local Date:", date)
timeStr := time.FormatTime(nano)
println("Local Time:", timeStr)
// Set custom timezone offset manually (e.g. UTC-3)
time.SetTimeZoneOffset(-180)
println("New Local Time:", time.FormatTime(nano))
// Parse date and time strings (Always UTC)
parsedNano, err := time.ParseDate("2024-01-15")
if err != nil {
panic(err)
}
println("Parsed UTC Nano:", parsedNano)
// Perform date calculations
isToday := time.IsToday(nano)
println("Is Today (Local)?", isToday)
}TinyTime automatically detects the local timezone offset:
- Standard Go: Uses
time.Now().Zone(). - WASM: Uses JavaScript
Date.getTimezoneOffset().
Sets the manual timezone offset in minutes from UTC.
Returns the current active timezone offset in minutes.
All formatting functions apply the current timezone offset to display local time.
Formats a value into a date string: "YYYY-MM-DD".
int64: UnixNano timestamp.string: Valid date string (passthrough).
Formats a value into a time string "HH:MM:SS" (or "HH:MM" for int16).
int64: UnixNano timestamp.int16: Minutes since midnight.string: Valid time string (passthrough).
Formats a value into a date-time string: "YYYY-MM-DD HH:MM:SS".
Formats a value into a short date-time string: "YYYY-MM-DD HH:MM".
All parsing functions assume UTC input and return UTC timestamps.
Parses a date string ("YYYY-MM-DD") into a UnixNano timestamp at midnight UTC.
Parses a time string ("HH:MM" or "HH:MM:SS") into minutes since midnight UTC.
Combines date and time strings into a single UnixNano timestamp (UTC).
Returns the current Unix timestamp in nanoseconds (UTC).
Checks if the given UnixNano timestamp is today based on the local timezone offset.
Checks if the given UnixNano timestamp is in the past.
Checks if the given UnixNano timestamp is in the future.
Calculates the number of full days between two UnixNano timestamps.
Waits for the specified milliseconds then calls f. Returns a Timer that can be used to cancel the call.
Note: In WASM environments, the callback runs in the JavaScript event loop. Keep callbacks lightweight to avoid blocking the UI.
// Start a timer
timer := time.AfterFunc(1000, func() {
println("1 second passed!")
})
// Stop the timer before it fires
timer.Stop()When compiled for WebAssembly (GOOS=js GOARCH=wasm), tinytime automatically uses JavaScript's native Date APIs instead of bundling Go's time package.
# Build for WebAssembly
GOOS=js GOARCH=wasm go build -o app.wasm .Always use gotest to run tests:
gotestgithub.com/tinywasm/fmt
MIT.