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"]];
Related
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.
I have a NSString like #"2014-11-27T10:54:08.185Z"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate * date = [dateFormatter dateFromString:self.creationTime];
I tried a lot of different formatter strings, but the date is always nil.
Or is there another problem here?
The hour in your date string format seems to be in the 24h format.
So you need to use HH instead of hh in your dateFormat.
More info
Try
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.creationTime];
self.dateLabel.text = [formatter stringFromDate:date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Please try this...:)
I am trying to set the date format for something like "2013-12-02T12:15:43.182Z. I used that code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssS''Z'"];
How can i set this date format?
Ues this:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"%#",dateConverted);
This might work for you..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
This may help you,
NSDate *myDate = [NSDate date];
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"Required date format: %#",[df stringFromDate:myDate]);
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]];