Having trouble converting this NSString to NSDate [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?
I can't convert this NSString to an NSDate: 2010-03-06T10:06:00-05:00
To be honest, I can not decipher the Unicode docs, where they talk about generic wall time , standard/daylight time, offsets from GMT as a fallback, etc.
To show that I have made a decent effort, here is what I have as the dateformatter: yyyy-MM-dd'T'HH:mm:ssZZ:ZZ"
If I strip out the timezone such that the date becomes 2010-03-06T10:06:00 then I know what to do. But how many letter "Z"s do I need, and are they upper case? Should I be removing the colon from the -05:00 ? The timezone has been my achilles heel with these conversions.
Could someone please help me out?
Thank you!

Have a look at this ISO 8601 date formatter and see if that works for you.

Related

DateFormatter and components order when using regex [duplicate]

This question already has answers here:
Date formats from device based on locale
(5 answers)
Can you override NSDateFormatter 12 vs 24 hour time format without using a custom dateFormat
(1 answer)
Closed 5 years ago.
If I've understood correctly, if I want to customize how a date is displayed I have two options:
I can use regex such as "MM-dd-yyyy HH:mm"
I can set DateFormatter.Style to short, medium, etc.
In the second case I believe that the order of the components changes according to the specific country default. For example "06 Dec" automatically becomes "Dec 06" when switching to another language with that date default.
However that's not the case when using regex, correct? So this means that if I want to customize the date format with regex, I need to provide a specific regex for each country?

How do I convert a Time integer to a Date in Ruby? [duplicate]

This question already has answers here:
How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?
(6 answers)
Closed 9 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I'm getting this stamp from an external API:
1391655852
and I need to convert it to a Date. I have seen a lot of conversion pages on the internet and on SO, but they all seem to be going the other way. I'm not familiar with this integer format at all.
Use Time.at for this:
t = Time.at(i)
I'll definitely yield to #duck's answer - in my opinion, that's the best way to convert an integer to Time - but if you're explicitly looking for a Date, you'll want to do a bit more work:
require 'date'
t = Time.at(i)
date = t.to_date
In the above example, date will be of class Date.

Ruby/Rails - Convert Unix Timestamp to HH:MM am/pm with Timezone

Let's suppose I have the following data:
Unix timestamp: 1388935522
User Timezone: America/Los_Angeles
I currently have the following piece of code,
Time.at(1388935522).strftime("%I:%M %P")
which converts the Unix timestamp.
What is the best way to use the "User Timezone" (currently as a "string") to determine the offset (which would be -8:00) and display the time based upon the user's timezone (7:25 am).
Thanks very much!
You have basically two alternatives:
server-side approach
client-side approach using JavaScript
I explained both in this answer no more than a few days ago.

NSDateFormatter Can't format date [duplicate]

This question already has answers here:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
(6 answers)
Closed 9 years ago.
I have tried and tried and researched and tried again but I cannot format this
The date is:
"2013-08-08T11:10:09-07:00"
I've tried using "yyyy'-'MM'-'DD'T'HH':'mm':'ssZ" and a host of different permutations of this but to no avail. I think perhaps the server is sending the incorrect format of the timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'DD'T'HH':'mm':'ssZ"];
return [dateFormatter dateFromString:[dictionary objectForKey:key]];
Any clues?
Thanks in advance
For the non-localized ISO offset that you're using—e.g., -07:00—you need to use 5 Z characters in your format. So, for the source data given, the correct format string would be:
yyyy-MM-dd'T'HH:mm:ssZZZZZ
While the currently used technical standard doesn't require date and time separators to be escaped, as Jeff mentions in the comments, it's probably not a bad idea to do so, especially if your source date is coming from a server or something. There are discussions on making those replacements, like the letters, that would change for locale-specific date and time separators. It also doesn't hurt to escape them from a technical perspective, it's just harder to read:
yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ
For more information, the current date formatter is based on the Unicode Technical Spec #35. The date and time pattern specifications for #35 can be found at http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

iOS date format with microseconds strange behavior

I have a strange problem with parsing date string. I have a date formatter with format:
yyyy-MM-dd HH:mm:ss.SSSSSSZZ
and date string:
2012-11-09 10:47:01.999804+01
dateFromString method returns nil, but when I change date string to ie:
2012-11-09 10:47:01.989804+01
it works... Does anyone has idea why there is such limit for microseconds value and how can I properly parse dates like the one above?
I could parse that with regex and cut whole SSSSSS part, but generally sometimes I will need to compare dates so they would not be matching and it will cause more problems.
I had no end of niggly issues doing this, finally got it to work but stripped the point seconds off and used the format as follows
#define DATEFORMATSTRINGTIMEZONE #"yyyy-MM-dd HH:mm ZZZ"
a bit like you say. I have to admit a bit more into the project I realized this was a very difficult method of sharing dates and instead adopted epoch time which has saved all headaches I was having regarding timezones... I'd highly recommend it if you have the luxury of changing the incoming data format.
Whilst I'm not sure why yours doesn't parse, I would question the ZZ instead of the ZZZ at the end given you have +01 not +1?
I have finally resolved that issue.
I'm modifying date format and date string to remove microseconds so I can parse date properly. Then I just add microseconds parsed from original date string.

Resources