go中时间转换

测试代码

package main

import (
	"fmt"
	"time"
)

func main() {

	fmt.Println("Time parsing");
	//dateString := "2014-11-12T11:45:26.371Z"
	dateString := "Mon, 03 Jun 2019 10:18:27 +0800"
	time1, err := time.Parse(time.RFC1123Z, dateString);
	if err != nil {
		fmt.Println("Error while parsing date :", err);
	}
	fmt.Println(time1);
}

参考

Basic time format example

Go doesn’t use yyyy-mm-dd layout to format or parse a time. Instead, you format a special layout parameter

Mon Jan 2 15:04:05 MST 2006

the same way as the time or date should be formatted. (This date is easier to remember when written as 01/02 03:04:05PM ‘06 -0700.)

const (
    layoutISO = "2006-01-02"
    layoutUS  = "January 2, 2006"
)
date := "1999-12-31"
t, _ := time.Parse(layoutISO, date)
fmt.Println(t)                  // 1999-12-31 00:00:00 +0000 UTC
fmt.Println(t.Format(layoutUS)) // December 31, 1999

The function

They have the following signatures:

func Parse(layout, value string) (Time, error)
func (t Time) Format(layout string) string
Common time and date layouts
LayoutNote
January 2, 2006
01/02/06
Jan-02-06
Date
15:04:05
3:04:05 PM
Time
Jan _2 15:04:05
Jan _2 15:04:05.000000
Timestamp
with microseconds
2006-01-02T15:04:05-0700
2006-01-02
15:04:05
ISO 8601 (RFC 3339)
02 Jan 06 15:04 MST
02 Jan 06 15:04 -0700
RFC 822
with numeric zone
Mon, 02 Jan 2006 15:04:05 MST
Mon, 02 Jan 2006 15:04:05 -0700
RFC 1123
with numeric zone
Predefined date and timestamp layouts

The following predefined format constants are also available.

ANSIC       = "Mon Jan _2 15:04:05 2006"
UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
RFC822      = "02 Jan 06 15:04 MST"
RFC822Z     = "02 Jan 06 15:04 -0700"
RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700"
RFC3339     = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen     = "3:04PM"
// Handy time stamps.
Stamp      = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano  = "Jan _2 15:04:05.000000000"
All layout options
TypeOptions
Year06 2006
Month01 1 Jan January
Day02 2 _2 (width two, right justified)
WeekdayMon Monday
Hours03 3 15
Minutes04 4
Seconds05 5
ms μs ns.000 .000000 .000000000
ms μs ns.999 .999999 .999999999 (trailing zeros removed)
am/pmPM pm
TimezoneMST
Offset-0700 -07 -07:00 Z0700 Z07:00
Corner cases

It’s not possible to specify that an hour should be rendered without a leading zero in a 24-hour time format.

It’s not possible to specify midnight as 24:00 instead of 00:00. A typical usage for this would be giving opening hours ending at midnight, such as 07:00-24:00.

It’s not possible to specify a time containing a leap second: 23:59:60. In fact, the time package assumes a Gregorian calendar without leap seconds.

原地址:

https://yourbasic.org/golang/format-parse-string-time-date-example/#common-time-and-date-layouts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值