Can't parse date from string - ios

I can't parse date from string via NSDateFormatter. The date string is #"2014-01-21T20:00:36+04:00". I am trying to use this format string: #"yyyy-MM-ddTHH:mm:ss" but it doesn't work. Please help me.
The full code is:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-ddTHH:mm:ssZ";
NSDate * date = [dateFormatter dateFromString:dateString];

add Z at the end #"yyyy-MM-dd'T'HH:mm:ssZZZZZ" as you have timezone included

Following on from http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
NSString *dateString = #"2014-01-21T20:00:36+04:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"Parsed date is %#", date);
yields the following output which seems correct
Parsed date is 2014-01-21 16:00:36 +0000

Related

Objective c : Get ISO Formated Date from String '2015-11-25T00:00:00.000Z'

In our app we are getting Date string "2015-11-25T00:00:00.000Z" from server.
Now we have to convert this string into into NSDate.
I've used the following code for conversion. Which is having an ISO Date Format.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *outputDate = [dateFormatter dateFromString:#"2015-11-25T00:00:00.000Z"];
But it returns null.
Can anyone please suggest where did I make the mistake...
Your code is correct.
It returns:
(lldb) po outputDate
2015-11-25 00:00:00 +0000
Try next code:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSDate *outputDate = [dateFormatter dateFromString:#"2015-11-25T00:00:00.000Z"];

NSDateFormatter dateFromString format string issue

I encountered a small problem today: I receive a couple of dates (NSString) from a web server. To use those dates correctly they need to be parsed into NSDate objects, for which I'm using the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyy HH:mm:ss";
return [formatter dateFromString:dateString]
The dates I am receiving are in the following format e.g.: #"02.06.2015 13:31:24".
My problem is that the above code returns nil. I think the issue probably is that I don't have the correct format string, which I have not been able to get right..
Any help would be highly appreciated!
You are missing a 'y' in your format. It should be:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyyy HH:mm:ss";
return [formatter dateFromString:dateString]
Please try below code -
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyyy HH:mm:ss";
return [formatter dateFromString:dateString]
-(NSDate *)getDateFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
return myDate;
}
Call it wherever required :)
[self getDateFromString:#"13.07.2013 16:22:11"];

How to get date in "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format?

i have date in this format 2014-09-20 04:45:20 +0000 and i need to convert this to yyyy-MM-dd'T'HH:mm:ss.SSSZ format.I tried with the following code
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2014-09-20 04:45:20 +0000"];
NSLog(#"%#",date);
but date giving me null value..
I am geting 2014-09-20 04:45:20 +0000 from time picker so anythg needs to be done on picker side will also help me.
Try this:
NSString *aStrDate = #"2014-09-20 04:45:20 +0000";
NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc] init];
aDateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z";
NSDate *aDate = [aDateFormatter dateFromString:aStrDate];
aDateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSLog(#"%#",[aDateFormatter stringFromDate:aDate]);

Regular expression for date formats in iOS

In my project i need to check some format coming from server, So i want to write a regular expression to check the format.
This is the format "03/31/2013 08:00:00" and other format is "03/31/2003 08:00 AM/PM"
How can i check this date formats. Can any one help me.
Regards
Kiran
^(?:\d{2}\/){2}\d{4} \d{2}:\d{2}(?::\d{2}| (?:AM|PM))?$
Note that it won't check for incorrect values.
If you want to get NSDate from this string, you should use NSDateFormetter instead:
NSString * date = #"03/31/2013 08:00:00"; // source string
NSDateFormatter * date_formatter = [[NSDateFormatter alloc] init];
[date_formatter setDateFormat: #"MM/dd/yyyy HH:mm:ss"]; // your date format
NSDate * result = [date_formatter dateFromString: date]; // converting
NSLog (#"%#", [result description]); // log your date result
[date_format release];
Not a real answer to your question about regex but, if you want to get the date from the dateString received from server you can try a trial and error method with the dateFormat of NSDateFormatter.
NSString *dateFormat1 = #"MM/dd/yyyy HH:mm:ss";
NSString *dateFormat2 = #"MM/dd/yyyy hh:mm:ss a";
NSString *dateString = #"03/31/2013 08:00:00 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:dateFormat1];
NSDate *date = [dateFormatter dateFromString:dateString];
if (!date) {
[dateFormatter setDateFormat:dateFormat2];
date = [dateFormatter dateFromString:dateString];
}
NSLog(#"Date : %#",date);

Weird converting output from NSString to NSDate

I have weird result trying to output NSDate object from NSString.
My NSString is: 1976-06-11
My method to convert is:
-(NSDate*)dateFromString:(NSString *)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateString];
return date;
}
But it output 1976-06-10 21:00:00 +0000
How could that happen? Difference in 1 day.
You have date in UTC format. Use this code to converting your date to local time:
NSTimeInterval seconds; // assume this exists
NSDate *ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *utcFormatter = [[NSDateFormatter alloc] init];
utcFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
utcFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSDateFormatter *localFormatter = [[NSDateFormatter alloc] init];
localFormatter.timeZone = [NSTimeZone timeZoneWithName:#"EST"];
localFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSString *utcDateString = [utcFormatter stringFromDate:ts_utc];
NSString *LocalDateString = [localFormatter stringFromDate:ts_utc];
Or you can use [NSTimeZone defaultTimeZone] to prevent hardcoded strings for timezone names. This method returns the system time zone, If no default time zone has been set.
You can use following methods to convert an UTC date string into UTC date and local date
- (NSDate *)convertIntoGMTZoneDate:(NSString *)dateString
{
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc]init];
[gmtFormatter setDateStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
return [gmtFormatter dateFromString:dateString];
}
- (NSDate *)convertIntoSystemZoneDate:(NSString *)dateString
{
NSDateFormatter *systemZoneFormatter = [[NSDateFormatter alloc]init];
[systemZoneFormatter setDateStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeZone:[NSTimeZone systemTimeZone]];
return [systemZoneFormatter dateFromString:dateString];
}
func dateFromString(dateString: String) -> NSDate {
// Convert string to date object
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormat.timeZone = NSTimeZone(name: "UTC")
var date = dateFormat.dateFromString(dateString)!
print(date)
return date
}
output :
1976-06-11 00:00:00 +0000
If you debug code, it shows 1 day difference but after run you will find the actual date which is you enter.
It works for me.I think it will helps you.
Thank you

Resources