12/04/2026 18:17น.

JS2GO EP.23 การทำงานกับ Date และ Time ใน JavaScript และ Go
#Date
#Time
#JavaScript
#Go
#JS2GO
การจัดการวันที่และเวลาเป็นเรื่องสำคัญในการพัฒนาโปรแกรม ตั้งแต่การแสดงผลเวลา การบันทึก log การจัดการ timezone ไปจนถึงการคำนวณเวลาระหว่างเหตุการณ์ การทำความเข้าใจวิธีจัดการ Date และ Time ใน JavaScript และ Go จะช่วยให้ระบบของคุณทำงานได้อย่างถูกต้องและแม่นยำ ในบทความนี้เราจะเปรียบเทียบการสร้าง แปลง และคำนวณวันที่และเวลา พร้อมตัวอย่างโค้ดและแนวทางปฏิบัติที่เหมาะสม
การจัดการ Date และ Time ใน JavaScript
JavaScript มี object Date สำหรับจัดการวันเวลา
สร้าง Date Object
// วันที่ปัจจุบัน
const now = new Date();
console.log(now); // e.g., 2025-09-10T07:30:00.000Z
// วันที่กำหนด
const birthday = new Date('1997-03-15T00:00:00');
console.log(birthday); // 1997-03-15T00:00:00.000Z
ดึงค่า Year, Month, Day
console.log(now.getFullYear()); // 2025
console.log(now.getMonth() + 1); // เดือน 1-12
console.log(now.getDate()); // วัน
การคำนวณเวลา
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
console.log(tomorrow);
การแปลงเป็น String
console.log(now.toISOString()); // 2025-09-10T07:30:00.000Z
console.log(now.toLocaleString()); // ขึ้นอยู่กับ locale ของระบบ
ข้อดี
✔️ ใช้งานง่ายและ built-in
✔️ รองรับ timezone พื้นฐาน
✔️ สามารถใช้ร่วมกับ library เช่น Moment.js, Day.js เพื่อความสะดวก
ข้อจำกัด
✔️ Month เริ่มจาก 0 → 11 ต้องระวัง
✔️ ไม่มี type-safe
✔️ การคำนวณซับซ้อนควรใช้ library
การจัดการ Date และ Time ใน Go
Go มี package time สำหรับจัดการเวลา
สร้าง 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
}
ดึงค่า Year, Month, Day
fmt.Println(now.Year()) // 2025
fmt.Println(now.Month()) // September
fmt.Println(now.Day()) // 10
การคำนวณเวลา
tomorrow := now.AddDate(0, 0, 1)
fmt.Println(tomorrow)
การแปลงเป็น 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
ข้อดีของ Go
✔️ Type-safe
✔️ Timezone management ชัดเจน
✔️ การคำนวณเวลาแม่นยำและปลอดภัย
ข้อจำกัด
✔️ Syntax verbose กว่า JavaScript
✔️ ต้องจำรูปแบบเวลาสำหรับ Format (2006-01-02 15:04:05 เป็น reference)
แนวทางปฏิบัติที่เหมาะสม
✔️ จัดการ Timezone ให้ถูกต้อง
- JavaScript:
.toLocaleString('en-US', { timeZone: 'Asia/Bangkok' }) - Go:
time.LoadLocation("Asia/Bangkok")และt.In(location)
✔️ ใช้ ISO 8601 สำหรับการแลกเปลี่ยนข้อมูล
- ช่วยให้ JSON, API และ database รู้จัก format เดียวกัน
✔️ หลีกเลี่ยงการ hard-code เดือน/วัน
- JavaScript:
getMonth() + 1 - Go: ใช้
time.Monthconstants เช่นtime.March
✔️ ใช้ library สำหรับงานซับซ้อน
- JavaScript: Moment.js, Day.js, Luxon
- Go: ใช้ package
timeร่วมกับ helper functions
สรุปเปรียบเทียบ Date & Time JavaScript vs Go
| Feature | JavaScript | Go |
|---|---|---|
| Object | Date | time.Time |
| Create | new Date() | time.Date(...) |
| Access | getFullYear(), getMonth(), getDate() | Year(), Month(), Day() |
| Calculation | setDate(), getTime() | AddDate(), Add() |
| Format | toISOString(), toLocaleString() | Format(time.RFC3339) |
| Timezone | limited | full support |
| Type Safety | No | Yes |
คำแนะนำ:
- Frontend / rapid prototyping → ใช้ JavaScript Date + libraries
- Backend / API / scheduling → ใช้ Go
timepackage เพื่อความถูกต้องและ type-safe
ตอนต่อไป
ใน EP.24 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้ การจัดการ String และ Text Processing ใน JavaScript กับ Go พร้อมตัวอย่างโค้ดและแนวทางปฏิบัติที่เหมาะสม เพื่อทำงานกับข้อความได้อย่างมีประสิทธิภาพ
อ่านบทความ Series อื่นๆ
🔵 Facebook: https://www.facebook.com/superdev.academy.th
🔴 YouTube : Superdev Academy
📸 Instagram: Superdev Academy
🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
🌐 Website: https://www.superdevacademy.com/