A minimal, portable time utility for Go and TinyGo with WebAssembly support. Automatically uses JavaScript Date APIs in WASM environments to keep binaries small.
import "github.com/tinywasm/time"
func main() {
tp := tinytime.NewTimeProvider()
// Get current Unix timestamp in nanoseconds
nano := tp.UnixNano()
println("Current time:", nano)
// Format dates and times
date := tp.FormatDate(nano)
println("Date:", date) // "2024-01-15"
timeStr := tp.FormatTime(int16(510)) // 8:30 in minutes
println("Time:", timeStr) // "08:30"
// Parse date and time strings
parsedNano, err := tp.ParseDate("2024-01-15")
if err != nil {
panic(err)
}
println("Parsed Nano:", parsedNano)
// Perform date calculations
isToday := tp.IsToday(nano)
println("Is Today?", isToday)
}Creates a time provider instance. Automatically selects the appropriate implementation:
- WASM: Uses JavaScript Date APIs (smaller binaries)
- Standard Go: Uses
timepackage
Formats a value into a date string: "YYYY-MM-DD".
int64: UnixNano timestamp.string: Valid date string (passthrough).
date := tp.FormatDate(1705306200000000000) // "2024-01-15"Formats a value into a time string.
int64: UnixNano timestamp ("HH:MM:SS").int16: Minutes since midnight ("HH:MM").string: Valid time string (passthrough).
timeStr := tp.FormatTime(int16(510)) // "08:30"Formats a value into a date-time string: "YYYY-MM-DD HH:MM:SS".
int64: UnixNano timestamp.string: Valid date-time string (passthrough).
dateTime := tp.FormatDateTime(1705307400000000000) // "2024-01-15 08:30:00"Formats a value into a short date-time string: "YYYY-MM-DD HH:MM" (without seconds).
int64: UnixNano timestamp.string: Valid short date-time string (passthrough).
dateTime := tp.FormatDateTimeShort(1705307400000000000) // "2024-01-15 08:30"Parses a date string ("YYYY-MM-DD") into a UnixNano timestamp at midnight UTC.
nano, err := tp.ParseDate("2024-01-15")Parses a time string ("HH:MM" or "HH:MM:SS") into minutes since midnight.
minutes, err := tp.ParseTime("08:30") // 510Combines date and time strings into a single UnixNano timestamp (UTC).
nano, err := tp.ParseDateTime("2024-01-15", "08:30")Returns the current Unix timestamp in nanoseconds.
nano := tp.UnixNano()Checks if the given UnixNano timestamp is today.
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 := tp.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 .
# Run tests in browser
go test -tags=wasmRun standard tests:
go test ./...Run WebAssembly tests:
GOOS=js GOARCH=wasm go test ./...For detailed browser testing instructions, see BROWSER_TEST.md.
github.com/tinywasm/fmt
MIT.