Parsing custom string gives wrong date - iphone

I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.

Remove the Greenwich Meridian time "+0000" from the first code

Related

Identifying date format

"2014-02-03T23:10:00Z"
I'm aware of what each of the numbers represents, but the T and Z throw me off. Does anyone know what type of date format this is? I'd rather use a native parser if I could find one.
Thanks
It is an ISO 8601 format (http://en.wikipedia.org/wiki/ISO_8601).
T is Time delimeter while Z is used if time zone is UTC.
It appears to be ISO 8601 format. RestKit has a parser for it. You should also be able to get NSDateFormatter to parse it.
"2014-02-03T23:10:00Z" is the ISO 8601 format (http://www.w3.org/TR/NOTE-datetime)
the T represents the beginning of the time and the Z represents that the date is expressed in UTC (Coordinated Universal Time)
if you want to get the date with a specific format you could use NSDateFormatter
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd"];
NSString *stringDate = [dateFormatter stringFromDate:date];
NSLog(#"%#", stringDate); // 2014/03/03
If you need just a certain part of the date (e.g just the current month):
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM"];
NSInteger *monthNumber = [[dateFormatter stringFromDate:date] integerValue];
NSLog(#"%i", monthNumber); // 03
If you need the date in a specific language:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd' de 'MMMM' del 'yyyy"]; // You can add your own text between single quotes
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es_ES"]];
NSString *stringDate = [dateFormatter stringFromDate:date];
NSLog(#"%#", stringDate); // 03 de febrero del 2014

NSDateFormatter formatting issue

I am trying to convert NSString into NSDate in 12 hours format. (in iOS 6)
Code :
NSString *Bdt = #"05/23/2012 08:00 AM"
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSLog(#"%#",bd);
Output:
2012-05-23 08:00:00 +0000 it should be 2012-05-23 08:00 AM
Whats wrong in the code ?
Thanks
If you want date in 2012-05-23 08:00 AM style
Create a dateformatter and setDateFormat as yyyy-MM-dd hh:mm a
NSString *Bdt = #"05/23/2012 08:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a" ];
NSString *datestr=[dateFormatter stringFromDate:bd];
NSLog(#"%#",datestr);
*Note : I am using ARC, so objects are not released/autoreleased.
EDIT:
NSDate will be in this format ONLY : 2012-05-23 08:00:00 +0000.
For any other format you need to use NSString.
Nothing is wrong with it. You are printing an NSDate in NSLog which is very different than creating an NSString with a specific format.
It seems your confusing the internal NSDate representation with string formatting. NSDate stores the date internally in a way different from how it is represented by humans. Just like NSString stores strings in a format that may not be what you ultimately want it encoded as, eg. ASCII or UTF-8. When you are calling NSLog you are getting a diagnostic message showing the date according to the string returned by - (NSString *)description or possibly - (NSString *)debugDescription.

How to convert Date like "2012-12-26" to "december 26, 2012" in iOS ?

I want to convert date 2012-12-26 to december 26, 2012 in iOS?
I am using websrvice and the data comes in this format 1990-12-26.
I want to change this to december 26, 2012 format.
This is what I am doing:
lbl_Rightside.text = [rootElement stringValueForNode:#"date"];
NSLog(#"lbl_Rightside is %#",lbl_Rightside.text);
[lbl_Rightside release];
Getting date to this label on 1990-12-26. Now I want to change date to december 26, 2012 format.
Any hints from experts would be very welcome.
you can use NSDateFormatter to do this kind of things. First
convert your date String to a date object using dateFromString:
method.
from date convert to string you want using stringFromDate: method
Different format strings can be found here.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:YOUR_ORIGINAL_STRING];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *finalString = [dateFormatter stringFromDate:orignalDate];
[dateFormatter release]; //if not using ARC
Check the official Apple documentation about NSDateFormatter. You should use this class to do this kind of formatting.
by using NSDateFormatter
NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Try to look at NSDateFormatter Class,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; // this is your input date format
NSDate *newDate = [dateFormatter dateFromString:dateString];//converting string to date object
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy"]; // setting new format
NSLog(#"The date is = %#",[dateFormatter stringFromDate:newDate])

NSDateFormatter format displaying incorrect format

I have the date (as an NSString) of the format:
20120508T224500Z
I am trying to use NSDateFormatter to create an NSDate from the string:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddTHHmmssZ"];
NSDate *dateFromString = [[NSDate alloc] init];
But this parses/formats the NSDate incorrectly.
I am trying to get the format:
4:45 PM 5/8/2012
How can I use the date formatter yyyyMMddTHHmmssZ to get this format?
You must single quote the 'T' and 'Z' in the string: 20120508T224500Z
See unicode date formatting for formatting information.
Example:
NSString *dateString = #"20120508T224500Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd'T'HHmmss'Z'"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateFromString: %#", dateFromString);
[dateFormatter setDateFormat:#"h:m a M/d/yyyy"];
NSString *newDataString = [dateFormatter stringFromDate:dateFromString];
NSLog(#"newDataString: %#", newDataString);
NSLog output:
dateFromString: 2012-05-09 02:45:00 +0000
newDataString: 10:45 PM 5/8/2012
Note that NSLog used the NSDate description method and displays the date/time in local time.
Use - (void)setTimeZone:(NSTimeZone *)tz to create the string in a different timezone.

Convert NSString date to NSDate or a specific string

Could you please help me with such converting
I have NSString date like #"2/8/2012 7:21:09 PM"
And I need to have such string in output:
"at 7:21 PM, february 8"
I've tried to use dateFormatter with different date patterns but it always return me null..I really don't understandd where I'm doing wrong :(
NSString *dateString = newsItem.Date;//Which is equal to #"2/8/2012 7:21:09 PM"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm/dd/yyyy hh:mm:ss"];//I'm sure that this patternt is wrong but have no idea how to write the right one
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSLog(#"date:%#", dateFromString);
You'll need to use two different date formatters. First one to convert the string in to a date, the second one to output the date as a string with the specified format.
NSString* dateString = #"2/8/2012 7:21:09 PM";
NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[firstDateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* date = [firstDateFormatter dateFromString:dateString];
NSLog(#"Date = %#",date);
NSDateFormatter* secondDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[secondDateFormatter setDateFormat:#"h:mm a, MMMM d"];
NSString* secondDateString = [NSString stringWithFormat:#"at %#",[secondDateFormatter stringFromDate:date]];
NSLog(#"%#",secondDateString);
The trick is the date format strings. They use a format called the unicode date format, the specification can be found here.