Saving selected date and time into a plist - objective-c

I would like to know how to save a selected date and time from a date picker into a plist.

NSMutableArray *userArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSMutableDictionary *dict = [userArray objectAtIndex:0];
NSDate *date = [NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"MM/dd/yyy hh:mm a"];
[dict setObject:[dateformatter stringFromDate:date] forKey:#"LastLocalDataUpdate"];
[userArray writeToFile:plistPath atomically:YES];

NSUserDefaults is good enough for what you're trying to do:
NSDate *theDate = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:theDate forKey:#"Date"];

NSUserDefaults will do it just nicely.

Related

How to convert Date from JSON to NSDate

Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.

NSDate in iOS6 not the same as in iOS5

I had this code working for iOS5, but I just teste
NSDate *now = [NSDate date];
NSString *strDate = [[NSString alloc] initWithFormat:#"%#",now];
Does anyone know easy solution of rewriting this. The format of the date should be
dd mm yyyy
The correct way to format a date as a string is to use an NSDateFormatter. You can set the style to something appropriate for the user’s current locale with the -setDateStyle: method, or set the format to a particular string with -setDateFormat:.
Try this one , Definitely will get Proper solution:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];//as per your requirement
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"]; //as per your requirement
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"theDate:%# "theTime:%#" , theDate, theTime);

Turning NSDate into NSString [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert NSDate to NSString
convert string to nsdate
Currently I have this code. It's for adding events to the calendar.
[...]
event.startDate = [[NSDate date] dateByAddingTimeInterval:86400];
event.endDate = [[NSDate date] dateByAddingTimeInterval:90000];
What I need is the code to add to a spesific start date and end date, and that's where NSString comes in handy. But I've had no luck converting it so far.
Refer this code :
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];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Hope it helps you
convert NSDate to NSString as
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *string = [dateFormatter stringFromDate:date];
[dateFormatter release];
That's an example if you need the format like “Nov 23, 1937”:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle= NSDateFormatterMediumStyle;
NSString* string= [formatter stringFromDate: date];
Check out the reference for other styles. If you need another style that hasn't a constant, you can use the date format, in this case it's:
formatter.dateFormat= #"MMM dd, yyyy"; // same as medium style
But the preferred way is to use the style, use the format only if there isn't a propert style.

How to make a string from a NSDate variable as well as another NSString variable

I am playing around with making a unique ID for whenever a button is pushed in my application. I basically want it to be two NSString variables that I make concatenated together.
So far, what I have got it mostly working as it gives me no errors in the code itself, but it does only give me (null) as my text in my label.
Where I have the 2 is basically a placeholder for where I will eventually have a variable that will look up what is in the plist for the username.
My code for this is:
NSString *UserID;
NSDate *TimeNow;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddhhmm"];
UserID = [NSString stringWithFormat:#"",'Username'];
NSString *CurrentTime = [dateFormatter stringFromDate:TimeNow];
SessionID = [NSString stringWithFormat:#"%# %#", UserID, CurrentTime];
UniqueSessionID.text = SessionID;
You haven't initialised TimeNow. And your UserID is just being set to the empty string. To clean up that code, I would do this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddhhmm"];
NSString *CurrentTime = [dateFormatter stringFromDate:[NSDate date]];
NSString *SessionID = [NSString stringWithFormat:#"Username %#", CurrentTime];
UniqueSessionID.text = SessionID;
Update:
To have "Username" dynamic just do this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddhhmm"];
NSString *CurrentTime = [dateFormatter stringFromDate:[NSDate date]];
NSString *UserID = #"Some user";
NSString *SessionID = [NSString stringWithFormat:#"%# %#", UserID, CurrentTime];
UniqueSessionID.text = SessionID;
Then just change UserID to what you want the username to be.
I think that there should be:
NSDate *TimeNow = [NSDate date];
NSDate *dateFromString = [[NSDate alloc] init];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd hh:mm"];//this is the date format of dateFromString
NSString *CurrentTime = [dateFormatter stringFromDate:TimeNow];
[dateFromString release];
UserID = [NSString stringWithFormat:#"",'Username'];
SessionID = [NSString stringWithFormat:#"%# %#", UserID, CurrentTime];
UniqueSessionID.text = SessionID;

Cocoa/Obj-C - Dateformat change

I got an application wich read XML file and extract data of the nodes and put the data into textfields.
One of the node is <date>20110305162831</date>
In my textfield I got: 20110305162831
Whish is not very understandable for enduser...
How can i format it like:
2011/03/05 16:28:31
Is it possible?
Here is my AppControler.m code:
NSMutableArray* dates = [[NSMutableArray alloc] initWithCapacity:10];
NSXMLElement* root = [doc rootElement];
NSXMLElement* root = [doc rootElement];
NSArray* dateArray = [root nodesForXPath:#"//Report/ReportCreationDate" error:nil];
for(NSXMLElement* xmlElement in dateArray)
[dates addObject:[xmlElement stringValue]];
NSString * date = [dates objectAtIndex:0];
[dateTextField setStringValue:date];
[doc release];
[dates release];
If someone can help me, it would be great!
Thanks in advance
Miskia
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyyMMddHHmmss"];
NSDate* date = [dateFormatter dateFromString: #"20110305163031"];
[dateFormatter setDateFormat: #"yyyy/MM/dd HH:mm:ss"];
NSString* date_str = [dateFormatter stringFromDate: date];