NSDateFormatter and /'s - objective-c

I have a NSString that I want to load into a NSDate. The string looks like: 21/10/2011 5:42:02 PM
What I have written is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
The date ends up with a null value.
There is obviously something wrong with the date formatter but I am not sure what.
Any help would be greatly appreciated.

If the string doesn't match the format, then it will return null.
For example, here's an example that matches the format and one that doesn't:
NSString *dateString = #"11/09/2011 12:22:02";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"dateString; %#", date);
NSString *dateString2 = #"2011-11-09 12:22:02 +0000";
NSDate *date2 = [dateFormat2 dateFromString:dateString2];
NSLog(#"dateString; %#", date2);
This outputs:
2011-11-09 07:27:54.342 Craplet[45608:707] dateString; 2011-09-11 05:22:02 +0000
2011-11-09 07:27:54.343 Craplet[45608:707] dateString; (null)

Related

Date format changing issue Objective C

All,
I want date format in dd/MMM/yyyy format below is the code i am using.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:#"dd/MMM/yyyy"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
But in the dateString i am getting in Oct 21,2014 which is diffrent how can i get in "21/Oct/2014" format please let me know
The issue is that you're calling setDateStyle: instead of setDateFormat:
Use this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MMM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"%#", dateString);
This code outputs "22/Oct/2014"

NSString to NSDate returns wrong date

I try to convert my NSString to NSDate object, but NSDateFormatter returns me a strange value.
Here is code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
[dateFormat release];
date value is 2012-08-14 21:00 +0000. It is 3 hours difference between NSString value and NSDate value. I think I've missed something, but I don't know what.
This is what i use:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00:00 +0000"];
NSLog(#"\n\n DATE: %# \n\n\n", date);
The +0000 is timezone, so make sure you use your timezone, like +0400.
Edit:
If you can't change the string, you can use this code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
As i knew NSDate holds Grinwich time, so if you are in Moscow time zone, everything is wright
In objective c for NSDate if you did not set the setTimeZone, NSDate will take default timezone as localTimeZone. so if you need to get the exact date which you give as NSString string format, you need to setTimeZone as UTC. Follow the sample code, I guess it will be helpful for you.
NSDateFormatter *loacalformatter=[[NSDateFormatter alloc]init];
[loacalformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *localDate =[loacalformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"localDate :%#",localDate);
NSDateFormatter *UTCformatter=[[NSDateFormatter alloc]init];
[UTCformatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[UTCformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate =[UTCformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"UTCDate :%#",UTCDate);
UTCDate :2012-08-15 00:00 +0000 (GMT+00:00)
As suggested in the comments, if the date you receive is UTC then you need to convert it to your local timezone. Apple recommend you always use a properly configured NSDateFormatter when displaying dates, to handle localisation issues.
Here's some example code for turning an NSDate into an NSString:
NSDate *date = // initialised elsewhere
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
dateFormat.timeZone = [NSTimeZone localTimeZone];
dateFormat.timeStyle = NSDateFormatterShortStyle;
dateFormat.dateStyle = NSDateFormatterShortStyle;
dateFormat.locale = [NSLocale currentLocale];
NSString *dateAsString = [dateFormat stringFromDate:date];

How can I parse this string to NSDate with NSDateFormatter?

I have a date as a string in the format:
2010-12-31 20:21:00 +0200
What I'd like to do is parse this using NSDateFormatter to an NSDate object but I'm having difficulty matching the format properly.
NSDateFormatter *dateFormatter = NSDateFormatter.alloc.init;
[dateFormatter setTimeZone: NSTimeZone.localTimeZone];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss'Z'"];
[dateFormatter dateFromString: #"2010-12-31 20:21:00 +0200"] // returns nil ;
Can anybody help me find the correct format? thanks
This code works:
NSDateFormatter *dateFormatter = NSDateFormatter.alloc.init;
[dateFormatter setTimeZone:NSTimeZone.localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:#"2010-12-31 20:21:00 +0200"];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
And I just want you to know, kind sir, that my eyes bleed when I see this NSDateFormatter.alloc.init
NSDateFormatter* format = [[NSDateFormatter alloc]init];
[format setDateFormat:#"MM/dd/YYYY"];
NSString * currentDate = [format stringFromDate:[NSDate date]];
NSDate *date = [format dateFromString:currentDate];

create a NSDate from a string

I have an NSString like this: #"3/15/2012 9:15 PM" and I would like to convert it to NSDate, I have done like this:
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];
NSLog(#"%#", date); // date = null
Can you help me please, thanks.
Use the following solution
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy HH:mm a";
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#", date);
Edit:
Sorry, the format should be as follows:
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.
Edit: #2
Use as below. You would get exact time too.
NSString *str = #"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"MM/dd/yyyy hh:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
formatter.timeZone = gmt;
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Your formatter is wrong. According to the NSDateFormatter documentation it should be "MM/dd/yyyy h:mm a". You also probably want to set the locale as stated in the Date Formatting Guideline:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
Try this:
NSString *str = #"3/15/2012 9:15 AM";
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:enUSPOSIXLocale];
[formatter setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [formatter dateFromString:str];
NSLog(#"%#",date);
Outputs:
2012-03-19 12:37:27.531 Untitled[6554:707] 2012-03-15 08:15:00 +0000
Hmmm - you've gone wrong for a couple of reasons
the NSDateFormatter is strict and you have not been.
you didn't supply str to dateFromString:
NSString* str = #"3/15/2012 9:15 PM";
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm a"];
NSDate* date = [formatter dateFromString:str];
NSLog(#"%#",date);
NSString *str =#"3/15/2012 9:15 PM";
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"MM/dd/yyyy"];
NSArray *firstSplit = [str componentsSeparatedByString:#" "];
NSDate *dateSelected = [dateformatter dateFromString:[firstSplit objectAtIndex:0]];
[dateformatter setDateFormat:#"mm:ss"];
NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]];
NSLog(#"%#",[dateformatter stringFromDate:dateSelected2]);
NSLog(#"%#",[dateformatter stringFromDate:dateSelected]);

how to convert NSString to NSDate

i tried to convert this NSString date to NSDate
NSString *update_time= #"2012-03-09 14:54:30.0";
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss'.0'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:update_time ];
NSLog(#"Date: %#", dte);
but i'm getting Date: (null)
"GMT+4" is not a valid timzone name. Use timeZoneForSecondsFromGMT: instead:
//...
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:4 * (60 * 60)];
[dateFormat setTimeZone:timeZone];
//...
If you want to use timeZoneWithName:, you can get a list of valid timezone names using [NSTimeZone knownTimeZoneNames]. Those all have the form "Europe/Berlin" etc.
NSString *update_time= #"2012-03-09 14:54:30.0";
Missing the pointer
You're missing a pointer.
NSString * update_time= #"2012-03-09 14:54:30.0";
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss'.0'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+4"]];
NSDate *dte = [dateFormat dateFromString:update_time ];
NSLog(#"Date: %#", dte);