Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.
Related
I have a date string in JSON format, it looks like this:
2013-05-22T10:54:40.437
And I'm trying to convert it to NSDate.
- (NSString *)dateFromString:(NSString *)datestring{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
NSDate *date = [dateFormat dateFromString:datestring];
//date is nil here.
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *newString = [newDateFormatter stringFromDate:date];
NSLog(#"Date -- %#",datestring);
return newString;
}
But date is nil after dateFromString. Does anyone know what I did wrong here?
Thanks!
Use this date format:
yyyy-MM-dd'T'HH:mm:ss.SSS
Your code with little modification:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormat dateFromString:datestring];
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *newString = [newDateFormatter stringFromDate:date];
NSLog(#"Date: %#, formatted date: %#", date, newString);
gives me this output:
Date: 2013-05-22 08:54:40 +0000, formatted date: 05/22/2013
Edit: how "ikinci viking" pointed out in comment, escaping the dashes is unnecessary. Escaping removed from the code
Update for iOS 11+
ISO8601DateFormatter is the recommended way to parse internet date on iOS 10+.
However it has few issues preventing it from usage with the specific format from the question:
Milliseconds are supported only on iOS 11+
It does not work for dates without timezone (the case in question). Date string must contain timezone part (e.g. +00:00 or Z for "Zulu time" UTC)
If date strings contains both milliseconds and timezone (e.g. 2018-09-07T14:04:13.143Z), it can be used as follows (Swift example):
let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
Your date format is incorrect. It should be:
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss:SSS"];
The three capital 'S' means it will take the fractional part to 3 digits. Adjust it as you will.
If your date format is "2014-02-25T13:05:10+00:00" then,
your code should be like this....
NSURL *url = [NSURL URLWithString:#"your URL"];
NSString *str = [[NSString alloc] initWithContentsOfURL:url usedEncoding:Nil error:Nil];
NSDateFormatter *dateFor = [[NSDateFormatter alloc] init];
[dateFor setDateFormat:**#"yyyy-MM-dd'T'HH:mm:ss'+'SS:SS"**];
NSDate *yourDate = [dateFor dateFromString:str];
NSLog(#"Your Date: %#",yourDate);
-(void)viewDidLoad {
self.date = [dateForRFC3339DateTimeString:#"2015-01-18T01:00:00.000Z"];
}
-(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 a NSDate.
NSDate *result = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
return result;
}
In my project I have an NSString that contains data like this
NSString *s=#"20";
I want to convert this as "08:00 ". How can I do this?
I tried this one:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString* dateString = [dateFormatter stringFromDate:#"20"];
but I am getting this dateString as null value.
You input as NSString, need output as NSString.
So intermediate work of NSDate and NSDateFormatter are not required.
You can attain this by :
NSString *s=#"20";
NSString *time=[NSString stringWithFormat:#"%d:00",[s integerValue]%12];
Please try to use this one .I hope it will work fine...
NSString *s=#"20";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH"];
NSDate *date = [df dateFromString:s];
[df setDateFormat:#"hh:mm a"];
NSString *newTime = [df stringFromDate:date];
NSLog(#"Time : %#",newTime);
I have a UIDatePicker and I m getting the selected date from it in yyyy-MM-dd format.Now I want to convert it to MM/dd/yyyy format ..How can i do it ?
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:#""];
[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(#"%#",dateString2);
Im getting 2012-10-30 but I want 10/30/2012.
See Instruction or introduction with every line
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
df.dateFormat = #"MM/dd/yyyy";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MM/dd/yyyy"];
Just to see how it ends up:
NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date]; // Don't use leading caps on variables
NSLog(#"%#", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
The original accepted answer does a few things extra:
1) It allocates two NSDateFormatter instances which is not required. NSDateFormatter is expensive to create.
2) It is better to release date formatters explicitly when you are done with them rather than a deferred release using autorelease.
//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:#"MM/dd/yyyy"];
//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];
//release formatter
[formatter release];
- (void)showTime {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"HH:mm:ss"];
// you label
_time.text = [dateFormatter stringFromDate:now];
}
- (void)showDate {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
// you label
_date.text = [dateFormatter stringFromDate:now];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showTime;
self.showDate;
}
Enjoy
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
I use a code with options NSISO8601DateFormatWithDashSeparatorInDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:#"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];
NSDate *d = [NSDate date]; // or set your date
NSLog(#"date in format %#", [dateFormatter stringFromDate:d]); // date in format 2018-06-20
I have a string like that:
NSString *string = #"2012-06-14T00:00:00+03:00";
I tried this and get "2012-06-14 21:00:00 GMT". But i need "14.06.2012". Is it possible?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:string range:nil error:&error];
You'll need to create a second NSDateFormatter for the desired format (in this case "dd.MM.yyyy"), and use its stringFromDate: methods on your date object to get a string of the desired format. Try adding this code after your existing code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"dd.MM.yyyy";
NSString *result = [formatter stringFromDate:date];
I am reading in a file of strings. Each string looks something like:
!AIVDO,1,1,,B,11b4N?HPs0KLrBDIStg;q?w22<06,0*5A,21/10/2011 12:01:13 PM
What I have done so far is:
NSArray *arrayFromString = [theString componentsSeparatedByString:#","];
NSString *dateString = [tempArrayFromAISString objectAtIndex:7];
// dateString = #"21/10/2011 12:01:13 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
This returns an NSDate of (null).
The string is set ok because when I output it with NSLog it looks good.
The date formatter is ok because if I uncomment the line commented out and set the date string myself it works.
Any ideas as to why the string from the array does not work?
Thanks in advance for any help.
Change to
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss a"];
Your example contains a typo. arrayFromString≠tempArrayFromAISString
This works for me
NSString *theString = #"!AIVDO,1,1,,B,11b4N?HPs0KLrBDIStg;q?w22<06,0*5A,21/10/2011 12:01:13 PM";
NSArray *arrayFromString = [theString componentsSeparatedByString:#","];
NSString *dateString = [arrayFromString objectAtIndex:7];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"%#", date);
There was a newline character at the end because I was reading from a file. I fixed it with:
dateString = [dateString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
This gets rid of the end of line character.