NSDateFormatter returning nil with dateFromString - ios

I am using the following code to change yyyy-MM-dd to M-dd-yyyy using NSDateFormatter but NSLog is returning the value nil. Please help. Thanks in advance.
NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MM-dd-yyyy"];
NSDate *DateOfBirth=[format dateFromString:trimmedDOB];
NSLog(#"DATE IS %#",DateOfBirth);

NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
//Set your input format
[format setDateFormat:#"yyyy-MM-dd"];
//Parse date from input format
NSDate *dateOfBirth = [format dateFromString:trimmedDOB];
//Set your output format
[format setDateFormat:#"MM-dd-yyyy"];
//Output date in output format
NSLog(#"DATE IS %#",[format stringFromDate:dateOfBirth]);

Change your code like below:
NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *DateOfBirth=[inFormat dateFromString:trimmedDOB];
NSDateFormatter *outFormat = [[NSDateFormatter alloc] init];
[outFormat setDateFormat:#"MM-dd-yyyy"];
NSLog(#"DATE IS %#", [outFormat stringFromDate:DateOfBirth]);

NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *dateformatterIn = [[NSDateFormatter alloc] init];
[dateformatterIn setDateFormat:#"yyyy-MM-dd"];
NSDate *DateOfBirth=[dateformatterIn dateFromString:trimmedDOB];
NSDateFormatter *dateformatterOut = [[NSDateFormatter alloc] init];
[dateformatterOut setDateFormat:#"MM-dd-yyyy"];

Each date format corresponds to a single date format.
Since you have 2 date formats (one date string you are converting from and the one you are converting to) you need two date formatters.
So to convert from one date string to the other :
Create 'from' date formatter (format: yyyy-MM-dd)
Convert your source 'date string' into an 'NSDate object'
Create 'to' date formatter (format: MM-dd-yyyy)
Convert the 'NSDate object' you created in step 2 into the final 'date string'

Related

Unable to convert NSString to NSdate format

I want to convert my input string to particular NSDate format,
Here is the code I have tried.
NSString * result;
NSDate * resultDate;
NSDate *date = [dateFormat dateFromString:maxUpcomingDateString];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.locale=[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat1 setDateFormat:#"yyyy/MMM/dd"];
NSLog(#"maxUpcomingDateString %#",maxUpcomingDateString)
NSDate *d=[dateFormat1 dateFromString:maxUpcomingDateString];
result = [dateFormat1 stringFromDate:date];
resultDate=[dateFormat1 dateFromString:result];
NSLog(#"max1 date upcoming %#",result);
NSLog(#"max2 date upcoming %#", resultDate);
Output of my log shows:
maxUpcomingDateString 10 January 2017
max1 date upcoming 2017/Jan/10
max2 date upcoming (null)
I want the output date in the form of 2017/Jan/10. When I try to log the string it shows me the correct output but when I convert it back to NSDate it shows null.
nsstring to nsdate
NSString *dateString = #"01-02-2010";
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:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
nsdate to nsstring
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
add this code on dateFormat
[dateFormat setDateFormat:#"dd MMM yyyy"];
It will solve the problem.
Add this code after allocating the dateFormat object.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM yyyy"];

iOS NSDateFormatter returns old date from string

NSDateFormatter dateFromString fetched older date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateformatter dateFromString:#"2015-05-18"];
The above function returns date as 2015-05-17 11:39:18 +0000 instead of
2015-05-18
Actually you are printing NSDate, if you are printing Date then compiler automatically convert your date to string by converting using current timezone. if you want actual converted date in NSString form then add one more line that convert NSDate to NSString like as bellowed.
NSDateFormatter * dFormatter = [[NSDateFormatter alloc] init];
[dFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dFormatter dateFromString:#"2015-05-18"];
NSLog(#"String : %#",[dFormatter stringFromDate:date]);
Output :
2015-05-18
this is working code to show the current date...might be solve your problem..you should go for this...
NSDate *Date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateFormated = [dateFormatter stringFromDate:Date];
NSLog(#"%#", dateFormated);

How to convert user entered string as NSDate object

I am taking input from user as string and want to store it in date format but after printing back to console it give me NULL...
Here is what I did:
MyLog(#"\nPlease Enter Date");
scanf("%s",cDate);
stringDate= [NSString stringWithCString:cDate encoding:1];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate * incomeDate = [dateFormat dateFromString:sDate];
MyLog(#"Date Entered is %#",[dateFormat stringFromDate:incomeDate]);
Make sure you are entering date string in dd-MM-yyyy as i can see you are using [dateFormat setDateFormat:#"dd-MM-yyyy"];
You can use following code to convert from string to date, but make sure you use same date format which you are taking input from user
NSString *stringtoconvert = #"13-09-2014";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormat dateFromString:stringtoconvert];
MyLog(#"Date Entered is %#",date);

NSString to NSDate with format conversation issue

I've already searched StackOverflow.com for an answer to this question, still without any success. Already looked here:
iOS - Converting time and date to user time zone
Date Format - time zone
Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?
IOS how to set date format
Nothing of those worked.
So I have this NSString date format: 2014-05-07T10:28:52.000Z trying to convert it to NSDate, this is what I'm using:
+ (NSDate*)stringToDate:(NSString*)string format:(NSString*)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
return [dateFormatter dateFromString:string];
}
This is how I'm using it:
NSDate *date = [AppUtils stringToDate:youtube.postDate format:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
I've also tried those formats:
yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm.ss.ZZZ
yyyy-MM-dd'T'HH:mm.ssZZZ
How could I convert it to NSDate successfully?
Thanks in advance!
If the date format is fixed, what I do is below.
Replace T by space
Replace Z by blank
And then do formatting...
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
Now do the formatting using
yyyy-MM-dd HH:mm:ss
Edit 1
If you want to work with your case, use below
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Edit 2
I tried this and it is working.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Edit 3
To make working with your case use below.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Here are 2 methods that I use to convert RFC3339 Date string to NSDate,
And also Method for converting NSDate to RFC3339 Date string
Method for converting RFC3339 Date string to NSDate
+ (NSDate *)dateForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
return [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];}
Method for converting NSDate to RFC3339 Date string
+ (NSString *)RFC3339DateTimeFromDate:(NSDate *)aDate
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
return [rfc3339DateFormatter stringFromDate:aDate];
}

How to convert a RFC3339-String into a NSDate

i am trying to convert a RFC3339 formatted String Object to the corresponding NSDateObject :
A received String looks like this : 2012-10-29T00:00:00+01:00
I tried to use a NSDateFormatter but i dont know the correct specifiers ?
Any Ideas how to do this the right way ?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"yyyy-MM-ddThh:mm:ss???????"]; // ?????
NSDate *date = [dateFormat dateFromString:startAtString];
Inside one of my project I have this routine:
-(NSDate *)convertUTCStringToDate:(NSString *)utcDateString {
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss";
return [df dateFromString:utcDateString];
}

Resources