Go Date Conversion Processing

Asked 2 years ago, Updated 2 years ago, 56 views

I tried to format the time.Time type of Go and replace it with the original variable, but it didn't convert.

 hoge.CreatedAt, err=time.Parse ("January 2, 2006 15:04 PM"), hoge.CreatedAt.Format ("January 2, 2006 15:04 PM")

If you view hoge.CreatedAt here, it remains as it was before the conversion.
Pre-conversion date
2015-09-0108:12:00+0000UTC

Why isn't it converted?

go

2022-09-30 20:57

1 Answers

hoge.CreatedAt is not the string data of type time.Time, and changing the format of the time notation does not change the internal data (if it is the same time).
Therefore, if you want to change the format, you must have a new variable of type string such as str:=hoge.CreatedAt.Format("January 2, 2006 15:04 PM").

Also, the time data (the contents of the time.Time variable) is not a string and does not contain formatting information.
Therefore, you must use the Format() method to obtain the date/time string in the specified format at any time.

This post was edited based on @user9156's comment and posted as community wiki.

This post was edited based on @user9156's comment and posted as community wiki.


2022-09-30 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.