iOS Convert NSString to NSDate alway nil - ios

I'm trying to convert string to NSdate. The string date is "12/27/2014 07:42:00 AM +0000"
Here is my code:
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yyyy HH:mm:ss a";
date = [ dateFormatter dateFromString:dateString];
The date value is always nil. Any of you knows what canI be doing wrong?
I'll really appreaciate your help.

try
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a Z";
as HH is 24-hour and conflicts a. Z stands for the timezone offset.
have a look on the specifiers: http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yyyy hh:mm:ss a Z";
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
// see https://developer.apple.com/library/ios/qa/qa1480/_index.html
NSDate *date = [dateFormatter dateFromString:dateString];

NSString *str =#"12/27/2014 07:42:00 AM +0000";
NSDateFormatter *sdateFormatter = [[NSDateFormatter alloc] init];
sdateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[sdateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a Z'"];
NSDate *sdate = [sdateFormatter dateFromString:str];
NSLog(#"%#",[sdateFormatter stringFromDate:sdate]);

Related

dateFromString returns nil. Unable to convert the date to the desired format

This is my input nsstring 30/01/18 3:25 PM
I want the date in this format : 30/Jan/18 3:25 PM.
This is what i tried :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"dd/MM/yy HH:mm "];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm "];
NSString *finalDate = [dateFormatter stringFromDate:date];
Also i tried this :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yy HH:mm z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:releaseDate];
[dateFormatter setDateFormat:#"dd/MMM/yy HH:mm z"];
NSString *finalDate = [dateFormatter stringFromDate:date];
I am always getting date as nil !! What am i doing wrong?
Also : Time comes in both 12 hour format and 24 hour format. How do i handle both at the same time?
You check it below function, its working fine: -
-(void)DateChange
{
NSString *Input_Date =#"31/01/18 04:25 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/mm/yy hh:mm a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"dd/MMM/yy hh:mm a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 31/Jan/18 04:25 AM
Use this function written in Swift to change the date format. Its working not
func dateFormatterfromString(_ date: String!) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
let date = dateFormatter.date(from:date)!
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateString = dateFormatter.string(from:date)
return dateString
}

Convert Date into another format with time

I want to convert a date string "2017-03-25 19:10:00 +0000" in the "2017-03-25 07:10 PM".
But No any luck.Please help me.
Thanks
NSString *dateString = #"2017-03-25 19:10:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd h:mm a";
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *result = [dateFormatter stringFromDate:date];

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.

NSDateFormatter cant parse German date

I try to make my NSDateFormatter set its locale to German, but it fails. In the code below, while the dateFrom is successfully parsed, dateTo is not.
Note: In German it is Februar, not February.
NSLocale *germanLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"dd. MMMM" options:0
locale:germanLocale];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:formatString];
NSDate *dateFrom = [dateFormat dateFromString:#"20. September"];
NSDate *dateTo = [dateFormat dateFromString:#"20. Februar"];
Try setting locale of NSDateFormatter:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = germanLocale;
[dateFormat setDateFormat:#"dd. MMMM"];
NSDate *dateFrom = [dateFormat dateFromString:#"20. September"];
NSDate *dateTo = [dateFormat dateFromString:#"20. Februar"];

How to change date format from string to dd/mm/yy on iOS?

So i am parsing an xml file and display on screen the dates that i parse.
The dates are like : Thu , 31 May 2012 20:43:54 GMT.
How can i convert the date to this format : dd/mm/yy ?
i dont need the time.
Well it crashes.
I tried :
NSString *date = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:date];
NSString *dateText = [[NSString alloc] initWithString:[dateFormatter stringFromDate:myDate]];
Any ideas?
You can use NSDateFormatter
For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:yourString];
See http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns for dateformat patterns
The idea is to use NSDateFormatter to get a date from your input time string, then use a new dateformatter to get whatever new time string you want from the date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
Alternatively for the second part You can use NSDateComponents.
after:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
//Get NSComponents from date object..
[NSString stringWithFormat:#"%#/%#/%#",dateComponent.date,dateComponent.month,dateComponent.year];

Resources