View : 0

12/04/2026 18:17pm

JS2GO EP.23 Working with Date and Time in JavaScript and Go

JS2GO EP.23 Working with Date and Time in JavaScript and Go

#JS2GO

#Go

#JavaScript

#Date

#Time

Managing dates and times is crucial in software development — from displaying timestamps, logging, handling timezones, to calculating the duration between events. Understanding how to work with Date and Time in JavaScript and Go ensures your system operates accurately and reliably. In this article, we’ll compare creating, converting, and calculating dates and times, along with practical code examples and best practices.

 

Date and Time in JavaScript

 

JavaScript provides the built-in Date object to handle date and time operations.

 

Creating a Date Object

// Current date
const now = new Date();
console.log(now); // e.g., 2025-09-10T07:30:00.000Z

// Specific date
const birthday = new Date('1997-03-15T00:00:00');
console.log(birthday); // 1997-03-15T00:00:00.000Z

 

Accessing Year, Month, and Day

console.log(now.getFullYear()); // 2025
console.log(now.getMonth() + 1); // Month 1-12
console.log(now.getDate()); // Day

 

Calculating Dates

const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
console.log(tomorrow);

 

Converting to String

console.log(now.toISOString());    // 2025-09-10T07:30:00.000Z
console.log(now.toLocaleString()); // Depends on system locale

 

Advantages

✔️ Easy to use and built-in
✔️ Basic timezone support
✔️ Compatible with libraries like Moment.js or Day.js for added convenience

 

Limitations

✔️ Month index starts from 0 → 11 (careful!)
✔️ Not type-safe
✔️ Complex calculations may require a library

 

Date and Time in Go

 

Go provides the time package for handling date and time operations.

 

Creating a Time Object

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Println(now) // 2025-09-10 14:30:00 +0700 +07

    birthday := time.Date(1997, time.March, 15, 0, 0, 0, 0, time.UTC)
    fmt.Println(birthday) // 1997-03-15 00:00:00 +0000 UTC
}

 

Accessing Year, Month, and Day

fmt.Println(now.Year())       // 2025
fmt.Println(now.Month())      // September
fmt.Println(now.Day())        // 10

 

Calculating Dates

tomorrow := now.AddDate(0, 0, 1)
fmt.Println(tomorrow)

 

Converting to String

fmt.Println(now.Format(time.RFC3339))        // 2025-09-10T14:30:00+07:00
fmt.Println(now.Format("2006-01-02 15:04:05")) // Custom format

 

Advantages of Go

✔️ Type-safe
✔️ Clear timezone management
✔️ Accurate and safe date calculations

 

Limitations

✔️ More verbose syntax than JavaScript
✔️ Must remember reference time format for formatting (2006-01-02 15:04:05)

 

Best Practices

 

✔️ Handle Timezones correctly
JavaScript: .toLocaleString('en-US', { timeZone: 'Asia/Bangkok' })
Go: time.LoadLocation("Asia/Bangkok") and t.In(location)

 

✔️ Use ISO 8601 for data exchange
Ensures consistent format for JSON, APIs, and databases

 

✔️ Avoid hard-coding months/days
JavaScript: getMonth() + 1
Go: Use time.Month constants, e.g., time.March

 

✔️ Use libraries for complex tasks
JavaScript: Moment.js, Day.js, Luxon
Go: time package with helper functions

 


 

JavaScript vs Go: Date & Time Comparison

 

FeatureJavaScriptGo
ObjectDatetime.Time
Createnew Date()time.Date(...)
AccessgetFullYear(), getMonth(), getDate()Year(), Month(), Day()
CalculationsetDate(), getTime()AddDate(), Add()
FormattoISOString(), toLocaleString()Format(time.RFC3339)
TimezoneLimitedFull support
Type SafetyNoYes

 

Recommendation:
Frontend / rapid prototyping → Use JavaScript Date + libraries
Backend / API / scheduling → Use Go time package for accuracy and type-safety

 

Next Episode

 

In JS2GO EP.24, we’ll explore String and Text Processing in JavaScript and Go, including code examples and best practices for handling textual data efficiently.

 

Read more

🔵 Facebook: Superdev Academy

🔴 YouTube: Superdev Academy

📸 Instagram: Superdev Academy

🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH

🌐 Website: https://www.superdevacademy.com/en