Date Conversion in objective c? - iphone

I have an array containing the String objects with this format 22/04/2011.My question is that how can I change this string into the yyyy-mm-dd format. I am using NSDateFormatter but it's not working.

The real thing to know that when you convert a string date to NSDate using NSDateFormatter using any of the above mentioned methods, you will always get the NSDate object in Local TimeZone. So when coding the method for date conversion you need to consider this factor.
Hope this helps..
-:samfisher

Try this,
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:#"22/04/2011"];
[formater setDateFormat:#"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"Date - %#",result);
And For Reference - http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html

Here how I do this ... This code works ... Try this
NSDate* currentDate = [NSDate date];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-mm-dd"];
NSString* currentDateString = [df stringFromDate:currentDate];
[df release];

NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];

Related

How to change the date format in iphone sdk

I'm getting Response from Webservices Date like "2012-08-17T00:00:00". I want to Display Date only like 17-08-2012. How Change Date format and Remove that time...
//String froms web service(ws)
NSString *stringFromWS =[NSString stringWithString:#"2012-08-17T00:00:00"];
//date formatter for the above string
NSDateFormatter *dateFormatterWS = [[NSDateFormatter alloc] init];
[dateFormatterWS setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date =[dateFormatterWS dateFromString:stringFromWS];
//date formatter that you want
NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:#"dd-MM-yyyy"];
NSString *stringForNewDate = [dateFormatterNew stringFromDate:date];
NSLog(#"Date %#",stringForNewDate);
also refer the link for more explanation
http://mobiledevelopertips.com/cocoa/date-formatters-examples-take-2.html
NSDate *today = [NSDate date];
NSLog(#"Date today: %#", today);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
dateFormat.dateFormat = #"dd-MM-yyyy";
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"Date: %#", dateString);
Also see the below date and time format styles
NSDateFormatterNoStyle
NSDateFormatterShortStyle
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
NSDateFormatterFullStyle
You can set it like [dateFormat setDateStyle:NSDateFormatterShortStyle];

How to specify the conversion format of a date to a string

I am creating an app where the current date needs to be displayed in a label and automatically updated each day. I have looked at a few tutorials and they have all shown how to get the date in YYYY:DD:MM hh:mm, I am wondering how to change this format to DD/MMM/YYYY.
Any sample code or links to tutorials would be greatly appreciated.
Something like this:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateformatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
[myLabel setText:[formatter stringFromDate:today]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *dateString = [dateFormatter stringFromDate: dayDate];
//display dateString in label
[dateFormatter release];
You need to use the NSDateFormatter. For specfic formats look at
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];

Objective-C String(yyyy-mm-dd) to NSDate

My App is a JSON code which involves a date in string type. (Such as "2011-10-01").
I would like to know how I could conver it into NSDate?
It needs to be displayed in a different time format such as "1 October, 2011".
ex. this code doesn't work:
NSString *date1 = #"2010-11-12";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MMMM-dd"];
NSDate *date2 = [dateFormat dateFromString:date1];
NSString *strdate = [dateFormat stringFromDate:date2];
As Tug writes in his blog post on the subject:
Objective-C and iOS SDK provide a class to help formatting date
(marshaling and unmarshaling), this class is NSDateFormatter. No
surprise, the NSDateFormatter uses the Unicode Date Format
Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:publicationDate ];
[dateFormatter release];
where publicationDate in this case is an NSString.
Use NSDateFormatter. The appropriate method is dateFromString:. Take a look at the documentation :)
You can try this
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd MM yyyy"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSDate *someDate=[NSDate date];
NSString *targetDateString = [df stringFromDate:someDate];
NSLog(#"date:%#",targetDateString);

NSDate and NSString cocoa

For my iPhone app I got to convert NSDate object to string, and then convert the string back to NSDate object.. Can someone help me out? thank you!
Use to convert from NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Use to convert from NSString to NSDate.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: stringFromDate];
You need to check out NSDateFormatter this does exactly this, both directions.
convert NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Convert 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];

How can I convert string into date?

I have two strings:
date1 = 3-5-2014;
date2 = 4-2-2010;
I have to convert them in date and then compare them. I want the same format of date as in strings i.e., dd-mm-yyyy. How it can be done?
NSString *string=#"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];
enjoy...
You can extract NSDate objects from strings using the NSDateFormatter class. See the Date Formatters section Apple's Data Formatting Guide for a detailed explanation and sample code.
Try with below
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
here is the SO post
Convert NSString date to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:#"25-8-2010"];
NSLog(#"%#", date);
[dateFormatter release];
Hope this works for you
Cheers :)