Formatting Date Using NSDateFormatter Not Working - objective-c

I have this NSString that has a date and time that I want to format:
Jul 17, 2013 09:10 PM
I want to format it like this:
yyyy-MM-dd HH:mm
but it doesn't work.
This is the code I'm using:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
NSDate *_date = [formatter dateFromString:_dateToFormat];
NSLog(#"_date: %#", _date);
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *_newDate = [formatter stringFromDate:_date];
NSLog(#"_newDate: %#", _newDate);
NSLog returns:
_date: 2013-07-17 04:10:00 +0000
_newDate: 2013-07-17 04:10:00 +0000
Am I missing something? After formatting the date, I want to subtract 45 minutes from the formatted date using the code:
_newDate = [_date dateByAddingTimeInterval:-60*45];
NSLog(#"Subtracted date: %#", _newDate);
But this isn't my worry right now since the first step isn't working, which is to format the date from NSString. Any idea why it isn't working?
Edit
Updated the typo on the code for dateByAddingInterval by changing _newDate with _date.

Since your dateString is in AM/PM format you will have to use
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];

you are using HH and a in your dateFormat. HH means "hour in 24 hour format", and it looks like it takes precedence over a, the period (i.e. PM).
Use #"MMM dd, yyyy hh:mm a" with hh, which means "hour in 12 hour format" to convert your string into a NSDate.
NSString *_dateToFormat = #"Jul 17, 2013 09:10 PM"; // this is in local time zone! (mine is UTC+2)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy HH:mm a"];
NSDate *_date = [formatter dateFromString:_dateToFormat];
NSLog(#"wrong _date: %#", _date);
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
_date = [formatter dateFromString:_dateToFormat];
NSLog(#"correct _date: %#", _date); // this is in UTC, not in local time zone!
NSLog(#"correct _date: %#", [_date descriptionWithLocale:[NSLocale currentLocale]]); // this should be in your local timezone
Output:
wrong _date: 2013-07-17 10:10:00 +0000
correct _date: 2013-07-17 19:10:00 +0000 (in my timezone: 21:10, or 09:10 PM)
correct _date: Wednesday, July 17, 2013, 9:10:00 PM Central European Summer Time
Keep in mind that printing a NSDate usually prints in UTC. So if your timezone is different the logged NSDate will not match your input date. It will be off by the offset between your timezone and UTC.
You can print [_date descriptionWithLocale:[NSLocale currentLocale]] to see the time in your local timezone.
But this is just when you NSLog the NSDate. The NSDate is still correct, it's just the printed output that seems to be wrong.
To conclude, your code should look like this:
NSString *_dateToFormat = #"Jul 17, 2013 09:10 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
// create date from string
NSDate *_date = [formatter dateFromString:_dateToFormat];
// subtract 45 minutes
_date = [_date dateByAddingTimeInterval:-60*45];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// turn date into string
NSString *_newDate = [formatter stringFromDate:_date];
NSLog(#"%#", _newDate);
Output: 2013-07-17 20:25

Use 2 different formatters, one for converting the string to a date and a second for converting your date back to a string. The first should use the en_US_POSIX locale and the second should probably use the system (default) locale (but if you want to force a particular format then you can use en_US_POSIX here too).

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy HH:mm a"];
NSDate *_date = [formatter dateFromString:_dateToFormat];
NSlog(#"Date: %#",_date);

(Posted answer on behalf of the question author).
I managed to make it work using this code with the appropriate output logs:
NSString *oldDate = #"Jul 17, 2013 09:10 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm a"];
NSDate *_date = [formatter dateFromString:oldDate];
NSLog(#"_date: %#", _date);
_date = [_date dateByAddingTimeInterval:-60*45];
NSLog(#"_date2: %#", _date);
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *_newDate2 = [formatter stringFromDate:_date];
NSLog(#"_newDate2: %#", _newDate2);
Output:
_date: 2013-07-17 13:10:00 +0000
_newDate: 2013-07-17 21:10
_date2: 2013-07-17 12:25:00 +0000
_newDate2: 2013-07-17 20:25

Related

NSDateFormatter dateFromString conversion

I am having problems with conversion of NSString object to NSDate. Here is what I want to do:
I am downloading an RSS Message from the internet. I have the whole message parsed by NSXMLParser. After that, I have parts like , or saved to particular NSStrings. I want to convert element (that includes publication date of RSS Message) to NSDate so that I could perform some operations on it like on a date object (e.g. sorting, showing on a clock etc.). Here is the way my looks like:
"Wed, 25 Sep 2013 12:56:57 GMT"
I tried to convert it to NSDate in this way:
*//theString is NSString containing my date as a text
NSDate *dateNS = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss ZZZ"];
dateNS = [dateFormatter dateFromString:theString];*
However, after doing above code, dateNS always appear to be (null).
My question is simple: what is the right way to convert NSString with date formatted like this to NSDate object?
By the way, I have seen the website
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
It seems that there are many ways to format particular date, but I could not find what I am doing wrong.
Your problem is the your date formatter has not identical fort as your date string:
You should set date formatter the same format like your date string
My Example:
// Convert string to date
NSString *beginString = #"Sat, 30 Dec 2013 14:45:00 EEST";
//beginString = [beginString stringByReplacingOccurrencesOfString:#"EEST" withString:#""];
//beginString = [beginString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Helsinki"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
//NSLog(#"Begin string: %#", beginString);
//NSLog(#"not formated: %#", dateFromString);
// Convert Date to string
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ru_RU"]];
[dateFormat setDateFormat:#"dd MMMM yyyy"];
myStrDate = [dateFormat stringFromDate:dateFromString];
[currentTitle setPubDate:myStrDate];
NSDate * dateNS = [[NSDate alloc] init]; is useless, you don't need to allocate any date object.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSLog(#"%#", [dateFormatter dateFromString:#"Wed, 25 Sep 2013 12:56:57 GMT"]);
Outputs the date correctly, are you sure theString isn't nil?

Convert date and time to local date and time format

I am facing issue while converting date and time in device's current (Local) date and time.
Am getting date in format 2013-04-03 01:07:56 +0000 from API and I want to convert this date in local date and time format and display. Can anyone please suggest me how can I achieve this?
Just set the timeZone in the dateFormatter, This code is enough
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];

How to get time with date string

How can i get the time from a datetime string according to my time zone. My time zone time is +5:30 GMT.The datetime looks like-
04/03/2013 3:30:00 AM
I want the output like-
9:00:00 AM
Thanks in advance.
You need two date formatters.
One to convert string to date. And then date to string to get required in time format.
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:#"04/03/2013 3:30:00 AM"];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setDateFormat:#"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
*Not compiled and check
If you have date and time in string which is date time in GMT then you need to add GMT in your string. By this you can get actual time of your time zone -
NSString *dateStr = #"04/03/2013 3:30:00 AM GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a ZZZ"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"hh:mm:ss a"];
NSString *opDateStr = [dateFormatter stringFromDate:date];
NSLog(#"op = %#",opDateStr);
NSString *strdate= #"04/03/2013 3:30:00 AM";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date=[dateFormatter dateFromString:strdate];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setDateFormat:#"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
NSLog(#"%#",time);
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
Output
06/03/2013 10:41:18 AM
[formatter setDateFormat:#"hh:mm:ss a"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
Output
10:41:18 AM
instead of [NSDate date] pass the required date to be formatted.
As you want the time from the time string you provided according to the current time zone, you need to do something like this
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a zzz"];
NSDate *date=[dateFormatter dateFromString:#"04/03/2013 3:30:00 AM 0000"];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setTimeZone:[NSTimeZone systemTimeZone]];
[dfTime setDateFormat:#"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
NSLog(#"%#",time);
you wont get the real output unless there is the offset present which is the 0000.(its the GMT offset). Also the time formatter wont work perfectly if the format of the dateString and its formatter are different. So you need to do it MM/dd/yyyy hh:mm:ss a zzz, zzz is the offset. Now your output will be 09:00:00 AM

Convert date time to NSDate from NSString

Time zone always made trouble for me. This time I have to convert date-time , that I have in NSSTring to NSDate.
I am doing something like this.
NSString *myStringDate=#"14-11-2012 4:09:00 PM +0500"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a Z"];
NSDate *aDate = [formatter dateFromString:myStringDate];
NSLog(#"%#",aDate);
but I am having the output like this
14-11-2012 11:09:00 +0000
Also, no AM/PM is setting :-(
What I want is 14-11-2012 4:09:00 PM +0500 i.e same Date-Time that I have in string.
Thanks in anticipation.
the output of your converting is giving the GMT time
NSString *myStringDate=#"14-11-2012 4:09:00 PM +0500"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a Z"];
NSDate *aDate = [formatter dateFromString:myStringDate];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:aDate];
NSString* ts_local_string = [df_local stringFromDate:aDate];
Here Is the Change need to be taken.
[formatter setDateFormat:#"dd-MM-yyyy h:mm:ss a "];
Thanks
The conversion looks correct, but the default output is in UTC time (+0000) and with a 24h clock which is why you don't get the AM/PM distinction (“01:00 PM” would be 13:00). You also need to format the output if you want a specific representation ([formatter stringFromDate:aDate])…
For the UTC issue you need to select the time zone of your choice, by giving that choice in place of [NSTimeZone systemTimeZone]. Try [NSTimeZone localTimeZone] for starters.

Change format of date

I got a NSString *string = #"2012-10-24 23:00:00 +0000";
And I want to convert that to a normal format 24 october 2012
I use the following code. But it keeps crashing. Can anyone help me?
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy MM dd HH:mm:SS zzz"];
NSDate *dateNS = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MM yyyy"];
date = [dateformat stringFromDate:dateNS];
[dateformat release]
It keeps crashing because it can't create the NSDate from the input date because the dateformat is incorrect. it missing the - between the date and the seconds are ss not SS.
Also you need a date formate with MMMM to get the months full name:
NSString *string = #"2012-10-24 23:00:00 +0000";
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateNS = [dateformat dateFromString:string];
[dateformat setDateFormat:#"dd MMMM yyyy"];
NSString *date = [dateformat stringFromDate:dateNS];
[dateformat release];