NSDate - dateFromString is returning nil - ios

I have an NSString that has a date. I'm trying to convert that date to an NSDate. When I do that I get nil fro the NSDate. Here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"dateString = %# date = %#", dateString, date);
Here is the output of the NSLog:
dateString = 2015-06-16 date = (null)
What am I doing wrong, and how can I fix it?

The date format does not match the date string. It needs to be: :#"yyyy-MM-dd"
The order and other characters need to match.
yyyy for a four digit year
MM for a two digit month
dd for a two digit day
See: ICU Formatting Dates and Times
Note: NSLog() uses the NSDate description method which presents date/time referenced to GMT (UTC) and NSDateFormatter defaults to your timezone so the date displayed may be different.

Related

NSDateFormatter showing wrong date in ios? [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 6 years ago.
i have NSString with this date format "2016-03-16" and i added following code to get the same date in proper NSDate format but its returning " 2016-03-15 18:30:00 +0000 ". How do do i get same "2016-03-16" in NSDate ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *datePart = [dateFormatter stringFromDate:dateFromString];//2016-03-16
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(#"%#----",dateValue); //" 2016-03-15 18:30:00 +0000
Your local time zone is presumably UTC+5:30. You are specifying a date but not a time, so the time is implied to be midnight. Midnight on the 16th in your local time zone is 18:30 the day before (the 15th) in UTC time, which is why you get "2016-03-15 18:30:00 +0000"
When you log the date with NSLog(#"%#----",dateValue) you are actually invoking [dateValue description], which displays the date using UTC.
You can use NSLog(#"%#----",[dateValue descriptionWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone] locale:nil]) and you will see the date in your current time zone.
Be aware though that the description and associated methods such as descriptionWithCalendarFormat methods are only for debugging, you should use an NSDateFormatter to convert dates to strings. iOS_Binod's answer shows one way you could do this.
You try to print NSDate instance in console. That's reason your code print default format value in console.
You need to get string value from NSDate instance with the help of this method [your_dateformater stringFromDate:dateInstance]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *datePart = #"2016-03-16";
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(#"string convert into Date is - %#",[dateFormatter stringFromDate:dateValue]);
The date formatter uses your local time zone by default. The -[NSDate description] method (which is what %# calls) uses UTC. This is why the strings are different.

Trying to get the current date as just month, day and year

I'm using the folowing code to get the date as a string
strStartDate.text=[mOnTimeApp->cDate descriptionWithLocale:nil];
It works but has the hour, minute and second. Is there a way to just get the Month, day and year as a string?
Passing nil into descriptionWithLocale: is the same as calling description on an instance of NSDate. NSDate Class Reference has the following to say about descriptionWithLocale:
Return Value
A string representation of the receiver, using the given locale, or if
the locale argument is nil, in the international format YYYY-MM-DD
HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours
and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”)
You can configure an instance of NSDateFormatter to represent the desired format and then use the method stringFromDate: passing in your NSDate object -
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
strStartDate.text = [formatter stringFromDate:mOnTimeApp->cDate];
Is this why you are looking for?
+ (NSString*)getAbsoluteDateAndTimeFromString:(NSNumber*)formattedTime
{
long long ts = [formattedTime longLongValue];
ts /= 1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:ts];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"M/d/yyyy h:mm a"];
return[dateFormatter stringFromDate:date];
}

Problems converting a string date to NSDate

I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);

Deviation from expected output with NSString to NSDate conversion using NSDateFormatter

I am using the below method to convert a NSString to NSDate.
Always when I construct the NSDate from String, the date is one day behind the current day I have provided as part of the input and hour is 18:30:00 +0000. Why this deviation from what I have provided. I was expecting to have the same date what I have provided and hour as 00:00:00 +0000
+(NSDate*)convertStringToNSDate:(NSString*)string withFormat:(NSString*)format{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:format];
NSDate *date = [dateFormat dateFromString:string];
[dateFormat release];
return date;
}
This question comes up quite regularly but I could not find a suitable duplicate (searching on the phone does not help).
NSDate represents a specific point in time. When you log the value of an NSDate it is displayed in GMT, which is 5.5 hours behind your timezone (India, I assume). So the value is correct. If you run that date back through your date formatter you will get the local time of midnight again, since the date formatter is using your local time zone.

NSDate formatting from NSString

I have data in NSString, I need to display it as Oct 3, 2011. I am having trouble in converting nsstring into NSDate and then again display it as NSString.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy "];
NSDate *dateFromString = [dateFormatter dateFromString:myString];
myString is 2011-10-3 00:00:00
First set the date formatter time zone. If the string you're receiving is GMT/UTC, set the timezone to that.
Next set the date format to match the incoming date pattern. Do dateFromString.
Then set the date format to match the desired output format. Also set the output timezone, if different. Do stringFromDate, using as input the NSDate object from the previous operation.

Resources