Convert String to date Objective-C - ios

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];

Related

Convert date string(2016-04-27T08:06:07.531Z) to specific date format dd.MM.YYYY

I have date string in the format 2016-04-27T08:06:07.531Z and want to convert into 2016.04.27. I have tried following code
NSString *dateString = #"2016-04-27T08:06:07.531Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
NSDate *dateReceived = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"dd.MM.YYYY"];
NSString *dateReq = [dateFormatter stringFromDate:dateReceived];
NSLog(#"Date Req:%#",dateReq);
I am getting null in console for NSLog. Any help will be appreciated. Thanks.
use like
NSString *dateString = #"2016-04-27T08:06:07.531Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateReceived = [dateFormatter dateFromString:dateString];
// 2016.04.27
[dateFormatter setDateFormat:#"yyyy.MM.dd"];
NSString *dateReq = [dateFormatter stringFromDate:dateReceived];
NSLog(#"Date Req:%#",dateReq);
Plz just change your date formatter's date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
instead of
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
try this it will help you --
NSString *date = #"2011-02-27T08:06:07.531Z";
NSDateFormatter *dFormat = [[NSDateFormatter alloc] init];
[dFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dReceive = [dFormat dateFromString:dateString];
[dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
NSString *dateReq = [dateFormatter stringFromDate:dReceive];
just change date format
[dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
Use date format as: [dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];

How to convert NSString to NSDate in IOS?

I am building an application where I need to convert The NSString to NSDate. I have googled and found a lot links of SOF mentioned below -
https://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-
back-again
Convert NSString to NSDate
Converting can NSString to NSDate format in objective C
I had used the answer given there but the problem I still facing is explain below step by step.
I had taken NSString *StringDate = #"01:00:00"; // I have declared time in the NSString variable.
I used the conversion method that mentioned in above link and stored the converted value to the NSDate variable.
When I am NSLog the NSDate variable. I am get "19:30:00" in my logcat. While the expected result should be "01:00:00".
Here is one of the code I am using
NSString *dateString = #"01:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
-(NSString *)geTimeFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"hh:mm a"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *stringFromTime = [dateFormatter stringFromDate:myDate];
return stringFromTime;
}
Try this
This is likely the correct date, but printed in a different timezone. I posted a longer explanation here:
Unexpected value from NSDate
BTW: In
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[[NSDate alloc] init] is completely meaningless, because you assign an instance of NSDate in the very next line.
Try this method to convert NSString to NSDate :-
- (NSDate*) convertStringToDate
{
NSString* dateStr = #"20/05/2015";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
return date;
}

iOS NSDateFormatter returns old date from string

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);

Which dateFormat should I choose while converting NSString to NSDate?

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...:)

Incorrect Output in NSDate dateFromString [duplicate]

This question already has an answer here:
NSDateFormatter Issues
(1 answer)
Closed 8 years ago.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-YYYY"];
NSDate *myTime = [[NSDate alloc] init];
myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
I am converting a stored date(currently hardcoded) into NSDate format so that I can display it and apply operations on it. But myTime is giving wrong output.
2013-12-21 18:30:00 +0000
whereas I am passing 07-22-2014 as string and expecting me to output 07-22-2014.
Do I need some formatting apart from setting it right. ? What is the reason of going wrong.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate* myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
try this
-(NSString *)dateFormater:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd hh:mm:ss";
NSString *strDate=[formatter stringFromDate:date];
formatter = nil;
return strDate;
}
use like this..
NSString *temp=[self dateFormater:[NSDate date]];
NSLog(#"%#",temp);

Resources