When I marshall time.Now()
to JSON object it gives result as "2009-11-10T23:00:00Z"
but printing time.Now
gives 2009-11-10 23:00:00 +0000 UTC
. Why are they different. What are T
and Z
. Also how can I convert it to swift NSDate object according to this table?
Asked
Active
Viewed 203 times
-3

Thellimist
- 3,757
- 5
- 31
- 49
-
have you even tried to search for it like for example "Z in timezone"? – Salvador Dali Jul 23 '15 at 21:44
-
T is just used to separate Time from fate. Z is the timezone (Zulu). – Mario Zannone Jul 23 '15 at 21:54
-
@MarioZannone Is there a need for `T`? why not just space? – Thellimist Jul 23 '15 at 21:57
-
3It's an ISO standard (ISO8601) – Mario Zannone Jul 23 '15 at 22:00
-
Anyway, in order to convert to an NSDate have a look at the NSDateFormatter documentation and [here](http://stackoverflow.com/questions/28791771/swift-iso-8601-date-formatting-with-ios7) – Mario Zannone Jul 23 '15 at 22:04
-
@MarioZannone I did try. The correct format looked like `yyyy-MM-dd'T'HH:mm:ssXX` but it doesn't work. `yyyy-MM-dd'T'HH:mm:ssZZZZ` didn't work either. The orijinal string I'm working on is `2015-07-21T05:34:00.448575Z` – Thellimist Jul 23 '15 at 22:15
-
1Look also [here](http://stackoverflow.com/questions/30269378/swift-converting-json-date-to-swift-compatible-date) – Mario Zannone Jul 23 '15 at 22:27
-
@MarioZannone Thank you this was it. You could post it as an answer :) – Thellimist Jul 23 '15 at 22:30
1 Answers
0
The meaning of those values is irrelevant, they're part of that format (ISO8601). There are a couple approaches to this. One is to define a custom MarshalJSON()
method for time or your struct and use it to format the date, the other is to represent it as a string in your struct in the first place so that when the default implementation executes you get the result you're looking for.
The method you'll ultimately need to use is; time.Format(format string)
The golang docs show this example for using it;
package main
import (
"fmt"
"time"
)
func main() {
// layout shows by example how the reference time should be represented.
const layout = "Jan 2, 2006 at 3:04pm (MST)"
t := time.Date(2009, time.November, 10, 15, 0, 0, 0, time.Local)
fmt.Println(t.Format(layout))
fmt.Println(t.UTC().Format(layout))
}
The medium date format in that link for example would use the string "Jan. 2, 2006"

evanmcdonnal
- 46,131
- 16
- 104
- 115
-
-
1@Thellimist because when marshal the json, the package uses reflection to iterate the types properties and format them for json. The a `time.Date` is formatted it's using the default format string. What I was trying to explain is that you can either avoid that by using a string in the struct you marshal, or you can implement that method so that when the `time.Date` is formatted it's using one of the ones you need. – evanmcdonnal Jul 23 '15 at 22:22
-
I understood your answer. I was wondering why they were different also. Wish I had asked as a separate question :) While reflection why doesn't it converts as a default format string – Thellimist Jul 23 '15 at 22:26