datetime


Examples of parsing, formatting, and getting unix timestamp of dates.

Node.js

  1. const nowUnix = Date.now()
  2. console.log(nowUnix)
  3. const datestr = '2019-01-17T09:24:23+00:00'
  4. const date = new Date(datestr)
  5. console.log(date.getTime())
  6. console.log(date.toString())
  7. const futureDate = new Date(date)
  8. futureDate.setDate(date.getDate()+14)
  9. console.log(futureDate.toString())
  10. const formatted = `${String(date.getMonth()+1).padStart(2, 0)}/${String(date.getDate()).padStart(2, 0)}/${date.getFullYear()}`
  11. console.log(formatted)

Output

  1. 1547718844168
  2. 1547717063000
  3. Thu Jan 17 2019 01:24:23 GMT-0800 (Pacific Standard Time)
  4. Thu Jan 31 2019 01:24:23 GMT-0800 (Pacific Standard Time)
  5. 01/17/2019

Go

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. nowUnix := time.Now().Unix()
  8. fmt.Println(nowUnix)
  9. datestr := "2019-01-17T09:24:23+00:00"
  10. date, err := time.Parse("2006-01-02T15:04:05Z07:00", datestr)
  11. if err != nil {
  12. panic(err)
  13. }
  14. fmt.Println(date.Unix())
  15. fmt.Println(date.String())
  16. futureDate := date.AddDate(0, 0, 14)
  17. fmt.Println(futureDate.String())
  18. formatted := date.Format("01/02/2006")
  19. fmt.Println(formatted)
  20. }

Output

  1. 1547718844
  2. 1547717063
  3. 2019-01-17 09:24:23 +0000 +0000
  4. 2019-01-31 09:24:23 +0000 +0000
  5. 01/17/2019