The problem of converting Ruby on Rails DateTime to NSDate

Why is this...

NSString *mydate = @"2011-07-20T23:59:00-07:00" NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSLog(@"%@", [[dateFormatter dateFromString:mydate] description]); 

... returns "(null)" in the console?

+6
source share
2 answers

The format of your date @"yyyy-MM-dd'T'HH:mm:ss'Z'" means that it searches for the letter character " Z " at the end of the line. However your line:

 @"2011-07-20T23:59:00-07:00" 

I don’t see there " Z ", right? Thus, the date is returned as nil , because your string does not match the specified format.

According to the date formatting documentation , you are probably looking for a format string:

 @"yyyy-MM-dd'T'HH:mm:ssZZ" 

However, even this may not work, because if you notice that your source line has a colon (" : ") between the hours and minutes of the time zone offset. There is no time zone specifier that takes this into account. If this is really a format in which ROR returns a date, then ROR is wrong.

+13
source

I am doing date transfer from Ruby to iOS through the JSON API, and I believe that I read the same article.

http://www.cimgf.com/2012/05/29/importing-data-made-easy/

I'm pretty sure they mistakenly added 'Z' as a string literal

-

To get Ruby for output, as you probably expect me to do the following:

  1.9.3-p392 :012 > s = Time.now => 2013-05-03 15:47:51 +0100 1.9.3-p392 :013 > s.strftime("%Y-%m-%d\T%H:%M:%S%:z") => "2013-05-03T15:47:51+01:00" 

Pay attention to%: z, which is available at least in Ruby 1.9.3

Is there a Ruby 1.8.7 time.strftime% z error?

0
source

Source: https://habr.com/ru/post/895373/


All Articles