Convert/Format DateString from NSUserdefaults - ios

I tried to format a String from NSUserDefaults, which holds a Date.
The result is (null).
My question is: Is there a way to convert this String into a date to format it, or is there a better way?
My Code:
NSString *c = [prefs objectForKey:#"expiration_date_full"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:c];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# %# Noch gültig bis: %#", a, b, dateFromString];

You can store an NSDate in NSUserDefaults without conversion. That is, NSDate is one of the types supported by PLISTs. You can check the documentation for more info.

change your date format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate* myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);

Related

Unable to convert NSString to NSdate format

I want to convert my input string to particular NSDate format,
Here is the code I have tried.
NSString * result;
NSDate * resultDate;
NSDate *date = [dateFormat dateFromString:maxUpcomingDateString];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.locale=[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat1 setDateFormat:#"yyyy/MMM/dd"];
NSLog(#"maxUpcomingDateString %#",maxUpcomingDateString)
NSDate *d=[dateFormat1 dateFromString:maxUpcomingDateString];
result = [dateFormat1 stringFromDate:date];
resultDate=[dateFormat1 dateFromString:result];
NSLog(#"max1 date upcoming %#",result);
NSLog(#"max2 date upcoming %#", resultDate);
Output of my log shows:
maxUpcomingDateString 10 January 2017
max1 date upcoming 2017/Jan/10
max2 date upcoming (null)
I want the output date in the form of 2017/Jan/10. When I try to log the string it shows me the correct output but when I convert it back to NSDate it shows null.
nsstring to nsdate
NSString *dateString = #"01-02-2010";
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:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
nsdate to nsstring
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
add this code on dateFormat
[dateFormat setDateFormat:#"dd MMM yyyy"];
It will solve the problem.
Add this code after allocating the dateFormat object.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM yyyy"];

Json date conversion in uilabel with keyValue

Hi in my project i get the date format in json i have tried many formats.it didnt worked out.ExpiryDate is my keyvalue stored in dictionary.please let me know the conversion for this.I need a format like this 07/Jan/2016.
cell.Expire.text=[ResDiction objectForKey:#"ExpiryDate"];
NSString *dateString = [ResDiction objectForKey:#"ExpiryDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
My json result:
CreateUserId = 2;
ExpiryDate = "2016-01-07T00:00:00";
Try this
cell.Expire.text=[NSString stringWithFormat :"%#",[ResDiction objectForKey:#"ExpiryDate"]];
Your conversion from sting to date seems to be correct.
However, you need to use another NSDateFormatter in order to output the date in the other format.
NSString *dateString = [ResDiction objectForKey:#"ExpiryDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"]; //Will convert from string to native date object
NSDate *date = [dateFormatter dateFromString:dateString];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MMM/yyyy"]; //will convert into string of type 07/Jan/2016
cell.Expire.text = [dateFormatter2 stringFromDate:date];
Hi i juz found the answer.Thanks for all your response
NSString * dateStr = [ResDiction objectForKey:#"ExpiryDate"];
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%#",datePart];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",newDateStr);
cell.Expire.text=newDateStr;

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

NSDate comparison in custom format Issue

I want compare two dates. I am getting device date in yyyy-dd-mm format and getting mm-dd-yyyy from webservices. I want to change device date format and get that changed date in NSDate object. Here is my code:
NSDate *today = [[NSDate alloc] init];
NSLog(#"today : %#", today);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM dd yy"];
NSString *tString = [dateFormatter stringFromDate:today];
NSLog(#"tString : %#",tString);
NSDate *date = [dateFormatter dateFromString:tString];
NSLog(#"date : %#",date);
You are saying date coming from webservice is in mm-dd-yyyy format..Then convert it into NSDate like this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSDate *convertedDate = [dateFormatter dateFromString:yourDateStringFromWeb];
You have device date already with you..Now you can compare two NSDate objects in many ways..See this and this..

Resources