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];
}
Related
I encountered a small problem today: I receive a couple of dates (NSString) from a web server. To use those dates correctly they need to be parsed into NSDate objects, for which I'm using the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyy HH:mm:ss";
return [formatter dateFromString:dateString]
The dates I am receiving are in the following format e.g.: #"02.06.2015 13:31:24".
My problem is that the above code returns nil. I think the issue probably is that I don't have the correct format string, which I have not been able to get right..
Any help would be highly appreciated!
You are missing a 'y' in your format. It should be:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyyy HH:mm:ss";
return [formatter dateFromString:dateString]
Please try below code -
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"dd.MM.yyyy HH:mm:ss";
return [formatter dateFromString:dateString]
-(NSDate *)getDateFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
return myDate;
}
Call it wherever required :)
[self getDateFromString:#"13.07.2013 16:22:11"];
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);
I searched the posts but didn't find something I could use...
What I try to do is: I want to save a date in CoreData (as an NSDate) but without the timezone identifier like +0000. The reason for that is, I use the date later as sorting path / title in a TableView and it just looks not so nice with the +0000 at the end.
I can not convert the date from CoreData as I use the "titleForHeader" function together with the NSFetchedResultsController, so I have no chance to convert that beforehand...
Or is it even possible to save the NSDate with a format like "dd-MM-yyyy / HH:mm" but still as NSDate() ?
I want do all this in Swift!
Thanks for any help...
NSDate doesn't contain any timezone information, you need to save the NSDate in Core Data then use an NSDateFormatter to format it for display.
It's no possible to save a NSDate without that. Only if your are going to save as an NSString and in that case you can use something like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
OR
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
Then save the string as:
[dateFormatter stringFromDate:YOUR_NSDATE_VAR];
Cheers.
This is worked for me:
NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:#"MaxDate"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat =#"yyyy-MM-dd'T'HH:mm:ss";
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:stringFromDate];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSString *newDateString = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", newDateString);
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'
I have seen the other answer and question but i didn't find any useful answer for me.
I have a string like 2014-10-07T07:29:55.850Z and I want to have a date to compare each other.
If I use this code:
NSString *fullDate = payload[#"sent_ts"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:fullDate];
My date is nil. Why?
Try This.
NSString *dateString = #"2014-10-07T07:29:55.850Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];