0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Convert UTC to JST with Carbon

Posted at

package main

import (
	"fmt"
	"time"

	"github.com/uniplaces/carbon"
)

func main() {
	tokyo := "Asia/Tokyo"
	// Parse date time from string
	startAt, _ := time.Parse("2006-01-02T15:04:05Z", "2019-10-07T06:54:08Z")
	endAt, _ := time.Parse("2006-01-02T15:04:05Z", "2019-10-07T06:54:08Z")
	// UTC to JST
	location, _ := time.LoadLocation("Asia/Tokyo")
	startAt = startAt.In(location)
	endAt = endAt.In(location)
	fmt.Println(startAt, endAt)
	// Convert to date range
	start, _ := carbon.CreateFromDate(startAt.Year(), startAt.Month(), startAt.Day(), tokyo)
	end, _ := carbon.CreateFromDate(endAt.Year(), endAt.Month(), endAt.Day(), tokyo)
	fmt.Println(start.StartOfDay(), end.AddDay().StartOfDay())
	// Convert to UTC date range
	fmt.Println(start.StartOfDay().In(time.UTC).String(), end.EndOfDay().In(time.UTC).String())

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?