nslocale issue in convert string to date - ios

I'm new to xcode, pls see below code and link
enter link description here
{
NSString * TempDate = [NSString stringWithFormat:#"%# %# %#",Datelbl.text,yearString,TimeLbl.text]; //Thursday, November 21 2013 5:35 PM
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat :#"EEEE',' MMMM d yyyy hh:mm a"];
AppointmentDate = [dateFormatter dateFromString:TempDate]; //NSDate
Appointment = [dateFormatter stringFromDate:AppointmentDate]; //NSString
}
Here , appointmentDate returns this value -> 2013-11-21 12:05:00 +0000,
Appointment returns this value-> Thursday, November 21 2013 05:35 PM.
I need to get 2013-11-21 05.35 PM. So,how to manage this GMT issue.

Try this one:
NSString *date = #"Thursday, November 21 2013 3:05 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat : #"EEEE, MMMM d yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *AppointmentDate = [dateFormatter dateFromString:date];
NSLog(#"DATE--> %#",AppointmentDate);
Output:
DAT--> 2013-11-21 15:05:00 +0000
Note: NSDate will be in 24 hour format. You need to convert it into string is you want as 03:05...

Edit :
[dateFormatter setDateFormat : #"EEEE, MMMM d yyyy hh:mm a"];
String value not converting to date format

Related

Covert string with timezone to Date

Here is the date string
NSString * dateString = #"Mon Sep 29 14:40:00 2014 PET";
How to convert it into NSDate?
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setCalendar:[NSCalendar currentCalendar]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Above code doesn't work.
what is the correct date format for above case?
the problem is your timezone definition (PET), because that is not on list of the recognisable timezones (please don't ask why not); this timezone is mostly known as UTC-5 in Apple's system, therefore:
NSString *_dateString = #"Mon Sep 29 14:40:00 2014 PET";
_dateString = [_dateString stringByReplacingOccurrencesOfString:#"PET" withString:#"UTC-5"];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[_dateFormatter setCalendar:[NSCalendar currentCalendar]];
[_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(#"date : %#", _date);
and the console says:
date : 2014-09-29 19:40:00 +0000
which is the correct representation of the original date in GMT+0 timezone.

NSString to NSDate with setDateFormat

I have a problem with NSDateFormatter parsing a string coming from the web.
I parse dates as string and transform them to NSDate. The problem is in choosing the correct format string.
Parsed dates have following "format":
Feb 04, 2014 8:00 AM ET
but I haven't find the correct format to transform them into an NSDate
I've tried with: EEE dd, yyyy hh:mm a but it is not working.
Code is simple:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd, yyyy hh:mm a"];
NSDate *articleDate = [formatter dateFromString:articleDateString];
Any idea?
Try below code to convert your NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm a 'ET'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *articleDate = [dateFormatter dateFromString:#"Feb 04, 2014 8:00 AM ET"];
NSLog(#"-->%#",articleDate);
Using this page: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change your "EEE" to "MMM" and add a "z" at the end to catch your timezone.
"EEE" is day of week, for example, "Tue", not month.

Converting NSString to NSDate

I have to convert Jan 24, 2014, 7:27:56 PM to NSDate
but facing problem to convert it due to Upper case PM/AM , i am using this code , but it giving me nil.
NSString *stringDate = #"Jan 24, 2014, 7:27:56 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(#"Date 1 : %#",date); //2013-02-28 12:00:00 +0000
Your formatter string contains sequence "yyyy hh", however the stringDate has "yyyy, hh". Try this:
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm:ss a"];

Strange date format behaviour , iOS

I am trying to convert a date from this :
2012-06-26 12:00:00 +0000
to this :
Jun 26, 2012 12:00:00 AM
If i use this code , it works :
NSString *inputString = #"2012-06-25 12:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"Output Date is: %#" , outputString);
and the date printed is correctly this : Jun 26, 2012 12:00:00 AM
However the thing gets really strange when i dont hardcode the date , but use a function to get it. The code :
NSString *inputString2 = [self.selectedDate description];
NSLog(#"%#",inputString2);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString2];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"Output Date is: %#" , outputString);
When i print in the beginning of the code the inpuString2 variable just to make sure that the date is correctly delivered by the function there i have this: 2012-06-25 12:00:00 +0000 which is the correct date. But now the output date is nil when printed!!
Any ideas?? how is this possible??
Its the date format string for formatter
You have
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];
Try instead
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Note: HH not hh and Z not z.
RE: the second formatter string - you could change that to
[formatter setDateFormat:#"MMM dd, yyyy HH:mm:ss a"];
(but I thought you were just asking about the nil)
Am assuming selectedDate date is NSDate, instead of converting use the following
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:self.selectedDate];
NSLog(#"Output Date is: %#" , outputString);
This may not answer your question, but it should give you correct date

Date Formatting gives null

I have string "24 Jan 2012 09:21:21 +0000" and I want to make it a NSDate object as
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:dateStr];
NSLog(#"%#",date);
gives me (null)
What i should do to get date in the format I want.
Since you have a specific date format, you can set your formatter up to match it.
dateString = #"24 Jan 2012 09:21:21 +0000";
[formatter setDateFormat:#"d MMM yyyy HH:mm:ss Z"];
date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
You can see the list of date specifiers here.
24 Jan 2012 09:21:21 +0000
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss ZZZ"];
I don't have an obj C environment to verify, but your date string is "year month day" and the input you want to handle is "day (short)month year"

Resources