NSDateFormatter formatting issue - iphone

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.

Related

Parsing custom string gives wrong date

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

NSString with date: reduce size of date

I have a NSString like Mon Feb 06 20:02:17 +0000 2012. I want to write it in a shorter way, maybe like: DD-MM-YY, HH:MM. I have think that maybe I can convert it to NSDate and again to a shorter NSString, but I don't know how to convert this strange date format to NSDate.
If you need more information, ask me.
Thanks!
You need to use an NSDateFormatter to tell NSDate the format the original date is in and the format you want it in, then you can convert between the two like this:
NSString *oldDate = #"Mon Feb 06 20:02:17 +0000 2012";
NSString *oldFormat = #"EEE MMM dd HH:mm:ss ZZ yyyy";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:oldFormat];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSString *newFormat = #"dd-MM-yy, HH:mm";
[dateFormatter setDateFormat:newFormat];
NSString *newDate = [dateFormatter stringFromDate:date];
There are similar question and answers in StackOverflow and a tutorial that may help you:
Tutorial:
http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Similar questions:
NSString to NSDate
Convert NSString->NSDate?
How to convert NSDate to NSString?
Convert NSString of a date to an NSDate
EDIT
NSString *dateStr = #"20081122";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
Try with NSDateFormatter.
You can convert to NSDate using one of its method:
- (NSDate *)dateFromString:(NSString *)string
And then convert that to whatever you want using NSDateFormatter.
Edit: Actually NSDate has a class method so you may check that as well:
+ (id)dateWithString:(NSString *)aString

NSDate from NSString gives null result

I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)

NSDate from NSString problems

Getting a NSString as follows:
NSString *lastSyncDate = [[items objectAtIndex:0] objectForKey:#"LastSyncDate"];
This value is: 1/1/2010 12:00:00 AM
I am using the following to convert to a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setDateFormat:#"MMMM d, y HH:mm:ss ZZZ"];
NSDate *mySyncDate = [df dateFromString:lastSyncDate];
This gives me a null value?
Your date format doesn't match the format of the string.
Try
[df setDateFormat:#"M/d/y hh:mm:ss a"];
or
[df setDateFormat:#"M/d/y h:mm:ss a"]; // if the hours aren't zero-padded
The format strings are a Unicode standard described at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
I agree with Seamus Campbell. However, be aware that setting the time style on the formatter (NSDateFormatter) will have no effect on the result returned by dateFromString.