time package Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/time-package/ Fri, 31 Jan 2020 18:26:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 time package Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/time-package/ 32 32 159787465 Convert time between different timezones in Go (Golang) https://vikasboss.github.io/convert-time-timezones-go/ https://vikasboss.github.io/convert-time-timezones-go/#respond Fri, 31 Jan 2020 18:24:50 +0000 https://vikasboss.github.io/?p=1266 Every time.Time object has an associated location value. When you change the location of any time.Time object to any other location, then that instant of time is not changed. Only the location...

The post Convert time between different timezones in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Every time.Time object has an associated location value. When you change the location of any time.Time object to any other location, then that instant of time is not changed. Only the location value associated with that time changes. The location associated with the time.Time object only has a presentation or display logic.

The In function can be used to change the location associated with a particular time.Time object. Whenever the In function is called on any time.Time object (say t) then,

  • A copy of t is created representing the same time instant.
  • t’s location is set to the location passed to In function for display purposes
  • t is returned back

Let’s see the below-working code which can be used to change the location value associated with a particular time.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()

    loc, _ := time.LoadLocation("UTC")
    fmt.Printf("UTC Time: %s\n", now.In(loc))
   
    loc, _ = time.LoadLocation("Europe/Berlin")
    fmt.Printf("Berlin Time: %s\n", now.In(loc))

    loc, _ = time.LoadLocation("America/New_York")
    fmt.Printf("New York Time: %s\n", now.In(loc))

    loc, _ = time.LoadLocation("Asia/Dubai")
    fmt.Printf("Dubai Time: %s\n", now.In(loc))
}

Output:

UTC Time: 2020-01-31 18:09:41.705858 +0000 UTC
Berlin Time: 2020-01-31 19:09:41.705858 +0100 CET
New York Time: 2020-01-31 13:09:41.705858 -0500 EST
Dubai Time: 2020-01-31 22:09:41.705858 +0400 +04

The post Convert time between different timezones in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/convert-time-timezones-go/feed/ 0 1266
Time/Date Formatting in Go https://vikasboss.github.io/time-date-formatting-in-go/ https://vikasboss.github.io/time-date-formatting-in-go/#respond Sat, 25 Jan 2020 17:48:30 +0000 https://vikasboss.github.io/?p=1225 If you have worked with time/date formatting in other languages you might have noticed that the other languages use special placeholders for time/date formatting. For eg ruby language uses %d for day...

The post Time/Date Formatting in Go appeared first on Welcome To Golang By Example.

]]>
If you have worked with time/date formatting in other languages you might have noticed that the other languages use special placeholders for time/date formatting. For eg ruby language uses

  • %d for day
  • %Y for year

etc

In Golang date and time format placeholders look like date and time only. Refer to below placeholder table

TypePlaceholder
Day2 or 02 or _2
Day of WeekMonday or Mon
Month01 or 1 or Jan or January
Year2006 or 06
Hour03 or 3 or 15
Minutes04 or 4
Seconds05 or 5
MilliSeconds  (ms).000        //Trailing zero will be included
or .999   //Trailing zero will be omitted
Micro Seconds (μs).000000             //Trailing zero will be included
or .999999        //Trailing zero will be omitted
Nano Seconds (ns).000000000        //Trailing zero will be included
or .999999999 //Trailing zero will be omitted
am/pmPM or pm
TimezoneMST
Timezone offset Z0700 or Z070000 or Z07 or Z07:00 or Z07:00:00  or -0700 or  -070000 or -07 or -07:00 or -07:00:00

It is easy to remember the above placeholders when represented in sequence.

Weekday-Month-Day-Hour-Min-Seconds-Year-Timezone will be Mon-01-02-03-04-05-06–07

Let’s see some time format code examples

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    
    //Format YYYY-MM-DD
    fmt.Printf("YYYY-MM-DD: %s\n", now.Format("2006-01-02"))

    //Format YY-MM-DD
    fmt.Printf("YY-MM-DD: %s\n", now.Format("06-01-02"))

    //Format YYYY-#{MonthName}-DD
    fmt.Printf("YYYY-#{MonthName}-DD: %s\n", now.Format("2006-Jan-02"))

    //Format HH:MM:SS
    fmt.Printf("HH:MM:SS: %s\n", now.Format("03:04:05"))

    //Format HH:MM:SS Millisecond
    fmt.Printf("HH:MM:SS Millisecond: %s\n", now.Format("03:04:05 .999"))

    //Format YYYY-#{MonthName}-DD WeekDay HH:MM:SS
    fmt.Printf("YYYY-#{MonthName}-DD WeekDay HH:MM:SS: %s\n", now.Format("2006-Jan-02 Monday 03:04:05"))

    //Format YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset
    fmt.Printf("YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset: %s\n", now.Format("2006-Jan-02 Monday 03:04:05 PM MST -07:00"))
}

Output:

YYYY-MM-DD: 2020-01-25
YY-MM-DD: 20-01-25
YYYY-#{MonthName}-DD: 2020-Jan-25
HH:MM:SS: 11:14:16
HH:MM:SS Millisecond: 11:14:16 .213
YYYY-#{MonthName}-DD WeekDay HH:MM:SS: 2020-Jan-25 Saturday 11:14:16
YYYY-#{MonthName}-DD WeekDay HH:MM:SS PM Timezone TimezoneOffset: 2020-Jan-25 Saturday 11:14:16 PM IST +05:30

The post Time/Date Formatting in Go appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/time-date-formatting-in-go/feed/ 0 1225