Convert NSString to NSDate [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I make a request to the flickr api and it returns me the date in the following format
"2013-02-01T06:25:47Z"
How do i convert this into NSDate format??

It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString
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];

You can use below function:
-(NSDate *)getDateFromString:(NSString *)pstrDate
{
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dtPostDate = [df1 dateFromString:pstrDate];
return dtPostDate;
}
Hope, it will be helpful to you.
This function will accept your string date and return the NSDate value.
Cheers!

Related

How to convert NSString to NSDate object [duplicate]

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 6 years ago.
I am receiving TimeString from server in such format - "2012-04-23T14:00:00.511Z" and for some reason I get nil when trying to convert that into NSDate object by doing following
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:00.511Z"];
What might be the issue?
Your code should be like,
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:51Z"];
NSLog(#"%#",myDate);

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

Convert String to date Objective-C

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

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

Getting null when trying to extract year value from a date [duplicate]

This question already has answers here:
NSDate from NSString gives null result
(3 answers)
Closed 9 years ago.
My code is the following:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);//null
How to fix that? thanx.
It is because you are using unix timestamp and you can convert unix timestamp into year or any date format as :
//Posted Date Format
NSString *dateStr = #"1339007317";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy"]; //Here you can set any date format Ex:#"dd MMM, yyyy hh:mm a" or#"dd/MM/yyyyy" according to your requirement
[dateFormatter1 setLocale:locale];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatter1 stringFromDate: date];
Hope it helps you.
You need two date formats. The first should match the format of the string you wish to convert to an NSDate. The second format needs to represent the format you want. Use the 2nd to convert the NSDate to the new string.
Also, why do you create a date object then immediately replace it with a new one?
Change this:
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
to:
NSDate *date = [dateFormatter dateFromString:dateString];
Your code needs to be like this:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???"]; // format that matches the dateString
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"yyyy"]; // the desired new format
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);

Resources