This question already has answers here:
NSDateFormatter dateFromString Always Returns nil
(7 answers)
Closed 6 years ago.
I have the date in the following format:
NSString *dateString = #"1996-08-09T07:00:00Z";
I need to extract only the year from the date above:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"date : %#", date);
Log:
2016-02-15 06:36:47.496 App[46714:5560483] date : (null)
I even tried:
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]
but no avail.
set date format as
1996-08-09T07:00:00Z
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
update
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *date = [dateFormat dateFromString:#"1996-08-09T07:00:00Z"];
NSLog(#"date : %#", date);
[dateFormat setDateFormat:#"yyyy"];
NSString *finalStr = [dateFormat stringFromDate:date];
NSLog(#"finalStr : %#", finalStr);
output
Try this function:
+(NSDate *)convertUTCDateLocal:(NSString*)inputString
{
NSString *dateFormat = #"yyyy-MM-dd'T'HH:mm:sszz";
//for input
NSTimeZone *inputTimeZone =[NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setTimeZone:inputTimeZone];
//NSString *inputString = #"2015-07-31T14:00:00+0000";
[inputDateFormatter setDateFormat:dateFormat];
NSDate *date = [inputDateFormatter dateFromString:inputString];
//for output
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[outputDateFormatter setLocale:[NSLocale currentLocale]];
[outputDateFormatter setDateFormat: #"yyyy"];
NSString *outputString = [outputDateFormatter stringFromDate:date];
NSDate *outputDate = [outputDateFormatter dateFromString:outputString];
return outputDate;
}
The format:
yyyy
does not match the date string:
1996-08-09T07:00:00Z
hence [NSDateFormatter dateFromString:] returned nil.
You want this format:
yyyy-MM-dd'T'HH:mm:ssZ
(Note the X specifier is documented as being the correct specifier for Z (Zulu Time) however during a discussion with #rmaddy, he has convinced me that Z is the correct one to use.)
and then to extract the year using NSDateComponents.
Related
i came across a problem in my iPhone application.
in my app i converte time string to date like that
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormat dateFromString:time];
and for some reason always date is nil after [dateFormat dateFromString:time].
can anyone tell me what i'm doing wrong?
thanks
I'm assuming your device language is other then English. In that case you need to set the date formatter Locale. Try this code.
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
try this...
NSString *time = #"01:30 pm";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
[dateFormat setDateFormat:#"hh:mm a"];
dateFormat.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
NSDate *date = [dateFormat dateFromString:time];
NSLog(#"date %#",[dateFormat stringFromDate:date]);
NSString *dats1 = #"01:30:00 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dats1];
NSLog(#"date : %#", date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date]);
I have datetime format like this
2015-10-06 12:39:12
when i try to convert into nsdate it return me null. i am using below code for it.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [formatter dateFromString:user_post.modification_date];
Please tell me how can i convert it into correct NSDate?
dateString is your date in string format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
Try to set like this
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
Change this line only.
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Z is the timeZone
I suggest you to set the timezone, can be the cause of the problem
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
I need to display today date format like 16 Mar,Monday,Here my code is
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM, EEEE"];
dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
I am getting output is 16MAR,MONDAY ,But my output should be 16 Mar, Monday.Thanks
Try this
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM,EEEE"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"Date String %#",dateString);
I am getting same output that you want
Hope you will got success..
I am using following code to get a date from string but its not showing correct date
NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];
[saveddf setDateFormat:#"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];
Saturday_June 22_2013 11:44 AM -- INPUT (datestring).
2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).
Please tell whats wrong in my code.
Try this code, it should work:
NSString *yourStringDate = #"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE_MMM dd_yyyy hh:mm a"];
NSDate *date = [dateFormatter dateFromString: yourStringDate];
//Set the required date format
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
Try to use this code
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateString);
I am having problem displaying proper date in my app. When user sets 24 hour format 'ON' in his date time preferences..my app shows correct time but when 24 hour format is set to 'OFF' it shows nothing.
Here is the code I am using:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *temp = [formatter dateFromString:strDate];
[formatter setDateFormat:format];
[formatter setLocale:[NSLocale currentLocale]];
NSString *returnStr = [formatter stringFromDate: temp];
[formatter release];
NSLog(#"Return: %#", returnStr);
return returnStr;
Try this
[dateFormat setDateFormat:#"hh:mm:ss"];
HH : 24 hour format
hh : 12 hour format
So in your case
//after conversion to date
if ON
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
if OFF
[formatter setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
Very simple try this:
For 12 Hrs : (hh:mm:ss)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
For 24 Hrs : (HH:mm:ss)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
use this:
Objective-C :
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
Swift 4.1
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
after that your date won't modified.
Set the locale of the dateFormatter as following. It should sove your problem.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatter setLocale:locale];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];