Detailed explanation of common methods of golang time

Time:2021-10-14

explain

In golang, time conversion will do a series of conversions through time type. It mainly includes time stamp and time string, and the time string is converted to another format. In short, all conversions are intermediate conversions through the time type. Reference documents: https://golang.org/pkg/time/

operation

1. Get the current time

?
1
2
t1 := time.Now()     // The time type returned is time
fmt.Println(t1, reflect.TypeOf(t1))

2. Get the specified time

?
1
2
t4 := time.Date(2019, 9, 30, 14, 28, 26, 23, time.Local)   // Return time format time
fmt.Println(t4)

3. Get the current timestamp

?
1
2
3
4
5
t1 := time.Now()
t1Second := t1.Unix()   // Gets the timestamp of seconds
fmt.Println(t1Second)
t1Nano := t1.UnixNano()     // Get millisecond timestamp
fmt.Println(t1Nano)

4. Timestamp to time.time

?
1
2
3
4
timeStamp := 1569826535
t := time.Unix(int64(timeStamp), 0)  // time.Time
strTime := t.Format("2006-01-02 15:04:05")
fmt.Println(strTime)  // 2019-09-30 14:55:35

5. Time string to time.time

?
1
2
3
4
5
6
7
S: = "15:10:30, September 30, 2019"   // ---> 2019-09-30 15:10:30
t. Err: = time. Parse ("January 2, 2006, 15:4:5", s)
if err != nil {
    fmt.Println(err.Error())
}
result := t.Format("2006-01-02 15:04:05")
fmt.Println(result)

Note: this place needs to pay attention to two points

1. The time template must be 15:4:5 on January 2, 2006. No matter how expressed, it will be this time.

2. When converting, what is the time format passed in and what must be written in the template time. For example:

Time with conversion: September 30, 2019 15:12:14   Then the template time should be   2006-01-30   15:04:05

Time conversion

Generally, the conversion process is intermediate conversion through time.time. Whether the timestamp format is converted to time format, or the time string is converted to time format / other time formats / timestamp, the conversion is done through time.time format.

1. Time format is converted to time stamp directly, because time type can be converted to any format and type of time

?
1
2
t1 := time.Now()
timeStamp := t1.Unix()

2. Timestamp to time string

?
1
2
3
4
timeStamp := 1569826535
t := time.Unix(int64(timeStamp), 0)  // time.Time
strTime := t.Format("2006-01-02 15:04:05")
fmt.Println(strTime)  // 2019-09-30 14:55:35

3. Obtain the month, day, hour, minute, second, week in the time

Either the given timestamp or time format is converted to time.time first

?
1
2
3
4
5
6
7
8
timeStamp := 1569826535  // 2019-09-30 14:55:35
t := time.Unix(int64(timeStamp), 0)  // time.Time
year, month, day := t.Date()
hour, minute, second := t.Clock()
fmt.Println(year, month, day, hour, minute, second)
//The method can also be provided according to the time format
//You can get the day of the week and the number of days in the current year
fmt.Println(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Weekday(), t.YearDay())

4. Time addition and subtraction method

?
1
2
3
4
5
6
7
8
9
10
timeStamp := 1569826535             // 2019-09-30 14:55:35
t := time.Unix(int64(timeStamp), 0) // time.Time
 
//Add or subtract the time (mm / DD / yyyy). If it is a negative number, how do you mean how much to subtract
t2 := t.AddDate(0, -1, 0)
fmt.Println(t2.Format("2006/01/02 15:04:05"))
 
//The second way of writing, the same negative number means subtraction
t3 := t.Add(-time.Hour * 24)
fmt.Println(t3.Format("2006/01/02 15:04:05"))

5. Judge the time

?
1
2
3
4
5
6
//Judge whether a certain time is before / after / equal to a certain time. The passed in parameters must be in time format
t4 := t.After(time.Now())
t5 := t.Before(time.Now())
t6 := t.Equal(t)
//The returned value is bool
fmt.Println(t4, t5, t6)    // false true true

This is the end of this article about the detailed explanation of common methods of golang time. For more information about common methods of golang time, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]