Cocoa/Obj-C - Dateformat change - objective-c

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

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.

Convert date in MM/dd/yyyy format in xcode?

I have a UIDatePicker and I m getting the selected date from it in yyyy-MM-dd format.Now I want to convert it to MM/dd/yyyy format ..How can i do it ?
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:#"%#",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:#""];
[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(#"%#",dateString2);
Im getting 2012-10-30 but I want 10/30/2012.
See Instruction or introduction with every line
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
df.dateFormat = #"MM/dd/yyyy";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MM/dd/yyyy"];
Just to see how it ends up:
NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = #"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date]; // Don't use leading caps on variables
NSLog(#"%#", dateString);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
The original accepted answer does a few things extra:
1) It allocates two NSDateFormatter instances which is not required. NSDateFormatter is expensive to create.
2) It is better to release date formatters explicitly when you are done with them rather than a deferred release using autorelease.
//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:#"MM/dd/yyyy"];
//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];
//release formatter
[formatter release];
- (void)showTime {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"HH:mm:ss"];
// you label
_time.text = [dateFormatter stringFromDate:now];
}
- (void)showDate {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
// you label
_date.text = [dateFormatter stringFromDate:now];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showTime;
self.showDate;
}
Enjoy
NSString *str = #"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
I use a code with options NSISO8601DateFormatWithDashSeparatorInDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:#"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];
NSDate *d = [NSDate date]; // or set your date
NSLog(#"date in format %#", [dateFormatter stringFromDate:d]); // date in format 2018-06-20

rfc 3339 to dd.mm.yyyy format

I have a string like that:
NSString *string = #"2012-06-14T00:00:00+03:00";
I tried this and get "2012-06-14 21:00:00 GMT". But i need "14.06.2012". Is it possible?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:string range:nil error:&error];
You'll need to create a second NSDateFormatter for the desired format (in this case "dd.MM.yyyy"), and use its stringFromDate: methods on your date object to get a string of the desired format. Try adding this code after your existing code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"dd.MM.yyyy";
NSString *result = [formatter stringFromDate:date];

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;

Saving selected date and time into a plist

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.