datetime转换成时间字符串

package main

import (
    "fmt"
    "reflect"
    "time"
)

func main() {
    now := time.Now()       // 当前 datetime 时间
    fmt.Println(reflect.TypeOf(now))    // 打印当前时间的类型
    fmt.Println(now)        // 打印当前时间
    fmt.Println("***********************")
    t1 := now.Format("2006-01-02 15:04:05")     // 把当前 datetime 时间转换成时间字符串
    fmt.Println(t1)
    fmt.Println(reflect.TypeOf(t1))
}

程序执行结果

time.Time
2020-12-28 17:51:01.2357834 +0800 CST m=+0.004988701
***********************
2020-12-28 17:51:01
string

datetime转换成时间戳

package main

import (
    "fmt"
    "time"
)

func parse_datetime_to_timestamp(d time.Time) int64 {
    unix_time := d.Unix()
    return unix_time
}
func main() {
    now := time.Now()                      // 当前 datetime 时间
    t1 := parse_datetime_to_timestamp(now) // 转换成时间戳

    tomorrow := now.AddDate(0, 0, +1)           // 一天之后的 datetime 时间
    t2 := parse_datetime_to_timestamp(tomorrow) // 转换成时间戳

    fmt.Println(t1)
    fmt.Println(t2)
}

程序执行结果

1568631868
1568718268

时间戳转换成时间字符串

package main

import (
    "fmt"
    "time"
)

func parse_timestamp_to_timestr(t int64, flag int) string {
    var time_str string
    if flag == 1 {
        time_str = time.Unix(t, 0).Format("2006.01.02")
    } else if flag == 2 {
        time_str = time.Unix(t, 0).Format("2006-01-02 15:04:05")
    } else if flag == 3 {
        time_str = time.Unix(t, 0).Format("2006-01-02 15:04")
    } else {
        time_str = time.Unix(t, 0).Format("2006-01-02")
    }
    return time_str
}

func main() {
    var t1 int64
    t1 = 1567778332
    str1 := parse_timestamp_to_timestr(t1,3)
    fmt.Println(str1)
}

程序执行结果

2020-12-28 21:58

时间字符串转换成时间戳

package main

import (
    "fmt"
    "time"
)

func parse_timestr_to_timestamp(time_str string, flag int) int64 {
    var t int64
    loc, _ := time.LoadLocation("Local")
    if flag == 1 {
        t1, _ := time.ParseInLocation("2006.01.02 15:04:05", time_str, loc)
        t = t1.Unix()
    } else if flag == 2 {
        t1, _ := time.ParseInLocation("2006-01-02 15:04", time_str, loc)
        t = t1.Unix()
    } else if flag == 3 {
        t1, _ := time.ParseInLocation("2006-01-02", time_str, loc)
        t = t1.Unix()
    } else if flag == 4 {
        t1, _ := time.ParseInLocation("2006.01.02", time_str, loc)
        t = t1.Unix()
    } else {
        t1, _ := time.ParseInLocation("2006-01-02 15:04:05", time_str, loc)
        t = t1.Unix()
    }
    return t
}

func main() {
    time_str1 := "2019-09-16 00:00:00"
    time_str2 := "2019-09-16 15:04"
    time_str3 := "2019-09-16"
    time_str4 := "2019.09.18"
    t1 := parse_timestr_to_timestamp(time_str1,5)
    fmt.Println(t1)

    t2 := parse_timestr_to_timestamp(time_str2,2)
    fmt.Println(t2)

    t3 := parse_timestr_to_timestamp(time_str3,3)
    fmt.Println(t3)

    t4 := parse_timestr_to_timestamp(time_str4,4)
    fmt.Println(t4)
}

程序执行结果

1568563200
1568617440
1568563200
1568736000

时间戳转换成datetime时间

package main

import (
    "fmt"
    "time"
)

func parse_timestamp_to_datetime(t int64) time.Time {
    return time.Unix(t,0)
}

func main() {
    var t1 int64
    t1 = 1568634050
    time1 := parse_timestamp_to_datetime(t1)
    fmt.Println(time1)
}

程序执行结果

2020-12-28 19:40:50 +0800 CST

时间字符串转换成时间戳

package main

import (
    "fmt"
    "time"
)

func parse_timestr_to_datetime(time_str string, flag int) time.Time {
    if flag == 1 {
        t, error1 := time.Parse("2006-01-02 15:04:05", time_str)
        if error1 != nil {
            panic(error1)
        }
        return t
    } else if flag == 2 {
        t, error2 := time.Parse("2006-01-02 15:04", time_str)
        if error2 != nil {
            panic(error2)
        }
        return t
    } else if flag == 3 {
        t, error3 := time.Parse("2006-01-02", time_str)
        if error3 != nil {
            panic(error3)
        }
        return t
    } else if flag == 4 {
        t, error4 := time.Parse("2006.01.02 15:04:05", time_str)
        if error4 != nil {
            panic(error4)
        }
        return t
    } else if flag == 5 {
        t, error5 := time.Parse("2006.01.02 15:04", time_str)
        if error5 != nil {
            panic(error5)
        }
        return t
    } else {
        t, err := time.Parse("2006.01.02", time_str)
        if err != nil {
            panic(err)
        }
        return t
    }
}

func main() {
    time_str1 := "2019-09-16 15:04:05"
    t1 := parse_timestr_to_datetime(time_str1, 1)
    fmt.Println(t1)

    time_str2 := "2019-09-16 15:04"
    t2 := parse_timestr_to_datetime(time_str2, 2)
    fmt.Println(t2)

    time_str3 := "2019-09-16"
    t3 := parse_timestr_to_datetime(time_str3, 3)
    fmt.Println(t3)

    time_str4 := "2019.09.16 15:04:05"
    t4 := parse_timestr_to_datetime(time_str4, 4)
    fmt.Println(t4)

    time_str5 := "2019.09.16 15:04"
    t5 := parse_timestr_to_datetime(time_str5, 5)
    fmt.Println(t5)

    time_str6 := "2019.09.16"
    t6 := parse_timestr_to_datetime(time_str6, 6)
    fmt.Println(t6)
}

程序执行结果

2020-12-28 15:04:05 +0000 UTC
2020-12-28 15:04:00 +0000 UTC
2020-12-28 00:00:00 +0000 UTC
2020-12-28 15:04:05 +0000 UTC
2020-12-28 15:04:00 +0000 UTC
2020-12-28 00:00:00 +0000 UTC

日期加减操作

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()        // 当前 datetime时间
    tomorrow := now.AddDate(0, 0, +1)    // 一天之后的时间
    fmt.Println(tomorrow)

    day_after_15 := now.AddDate(0, 0, -15)    // 15天之前的时间
    fmt.Println(day_after_15)
}

程序执行结果

2019-09-17 17:55:59.4755575 +0800 CST
2019-09-01 17:55:59.4755575 +0800 CST

总结:

import (
    "fmt"
    "time"
)

func parse_datetime_to_timestr(t time.Time, flag int) string {
    /**
        把 datetime 转换成 时间字符串
        t: datetime 时间,比如:2019-09-17 09:45:42.5962359 +0800 CST m=+0.003989201
        flag: 标识位,决定输出的时间字符串的格式
     */
    var time_str string
    if flag == 1 {
        time_str = t.Format("2006-01-02 15:04:05")
    } else if flag == 2 {
        time_str = t.Format("2006-01-02 15:04")
    } else if flag == 3 {
        time_str = t.Format("2006-01-02")
    } else if flag == 4 {
        time_str = t.Format("2006.01.02 15:04:05")
    } else if flag == 6 {
        time_str = t.Format("2006.01.02 15:04")
    } else {
        time_str = t.Format("2006.01.02")
    }
    return time_str
}

func parse_datetime_to_timestamp(t time.Time) int64 {
    /**
        把 datetime 转换成时间戳
        t: datetime 时间
     */
    return t.Unix()
}

func parse_timestr_to_datetime(time_str string, flag int) time.Time {
    /**
        把时间字符串转换成 datetime 时间
        time_str: 时间字符串,比如:2019-09-17 15:04:05
        flag: 标识位,决定输入的时间字符串的格式
     */
    if flag == 1 {
        t, error1 := time.Parse("2006-01-02 15:04:05", time_str)
        if error1 != nil {
            panic(error1)
        }
        return t
    } else if flag == 2 {
        t, error2 := time.Parse("2006-01-02 15:04", time_str)
        if error2 != nil {
            panic(error2)
        }
        return t
    } else if flag == 3 {
        t, error3 := time.Parse("2006-01-02", time_str)
        if error3 != nil {
            panic(error3)
        }
        return t
    } else if flag == 4 {
        t, error4 := time.Parse("2006.01.02 15:04:05", time_str)
        if error4 != nil {
            panic(error4)
        }
        return t
    } else if flag == 5 {
        t, error5 := time.Parse("2006.01.02 15:04", time_str)
        if error5 != nil {
            panic(error5)
        }
        return t
    } else {
        t, err := time.Parse("2006.01.02", time_str)
        if err != nil {
            panic(err)
        }
        return t
    }
}

func parse_timestr_to_timestamp(time_str string, flag int) int64 {
    /**
        把时间字符串转换成时间戳
        time_str: 时间字符串,比如:2019-09-17 09:45:42
        flag: 标识位,决定传入的时间字符串的格式
     */
    var t int64
    loc, _ := time.LoadLocation("Local")
    if flag == 1 {
        t1, _ := time.ParseInLocation("2006.01.02 15:04:05", time_str, loc)
        t = t1.Unix()
    } else if flag == 2 {
        t1, _ := time.ParseInLocation("2006-01-02 15:04", time_str, loc)
        t = t1.Unix()
    } else if flag == 3 {
        t1, _ := time.ParseInLocation("2006-01-02", time_str, loc)
        t = t1.Unix()
    } else if flag == 4 {
        t1, _ := time.ParseInLocation("2006.01.02", time_str, loc)
        t = t1.Unix()
    } else {
        t1, _ := time.ParseInLocation("2006-01-02 15:04:05", time_str, loc)
        t = t1.Unix()
    }
    return t
}

func parse_timestamp_to_timestr(stamp int64, flag int) string {
    /**
        把时间戳转换成时间字符串
        stamp: 时间戳,比如:1568685105,调用此方法时需先声明为 int64 类型
        flag: 标识位,决定输入的时间字符串的格式
     */
    var time_str string
    if flag == 1 {
        time_str = time.Unix(stamp, 0).Format("2006-01-02")
    } else if flag == 2 {
        time_str = time.Unix(stamp, 0).Format("2006-01-02 15:04:05")
    } else if flag == 3 {
        time_str = time.Unix(stamp, 0).Format("2006-01-02 15:04")
    } else if flag == 4 {
        time_str = time.Unix(stamp, 0).Format("2006.01.02 15:04:05")
    } else if flag == 5 {
        time_str = time.Unix(stamp, 0).Format("2006.01.02 15:04")
    } else {
        time_str = time.Unix(stamp, 0).Format("2006.01.02")
    }
    return time_str
}

func parse_timestamp_to_datetime(t int64) time.Time {
    /**
        时间戳转换成 datetime 时间
     */
    return time.Unix(t, 0)
}

func get_after_day(day_range, flag int) time.Time {
    /**
        获取多少天,多少月或者多少年之前或之后的时间
        day_range: 间隔的天数,月数或者年份数
        flag: 决定是取天数,月数还是年数
     */
    now := time.Now()
    var tmp_day time.Time
    if flag == 1 {
        tmp_day = now.AddDate(day_range, 0, 0)
    } else if flag == 2 {
        tmp_day = now.AddDate(0, day_range, 0)
    } else {
        tmp_day = now.AddDate(0, 0, day_range)
    }
    return tmp_day
}

func get_after_time(time_range string) time.Time {
    /**
        获取多少小时,分钟及秒之前或之后的时间
        time_range: 时间差,比如:
            10h     获取10小时之后的时间
            -10h     获取10小时之前的时间
            10m        获取10分钟之后的时间
            -10m    获取10分钟之后的时间
            10s        获取10秒之后的时间
            -10s    获取10秒之后的时间
     */
    m, _ := time.ParseDuration(time_range)
    tmp := time.Now().Add(m)
    return tmp
}

func get_current(t time.Time) {
    current_year := t.Year() //    获取年份
    fmt.Println(current_year)

    current_month := t.Month() //    获取月份
    fmt.Println(current_month)

    current_day := t.Day() //    获取天数
    fmt.Println(current_day)

    current_hour := t.Hour() //    获取小时数
    fmt.Println(current_hour)

    current_minute := t.Minute() //    获取分钟数
    fmt.Println(current_minute)

    current_second := t.Second() //    获取秒数
    fmt.Println(current_second)

    current_nanosecond := t.Nanosecond() //    获取纳秒数
    fmt.Println(current_nanosecond)
}

转自:https://www.cnblogs.com/renpingsheng/p/11532190.html