Datetime to date only in objective c - iphone

i have a date time like this
2012-01-01 10:00:00
i need to convert it into date only like this
2012-01-01
Can anyone help me please
this is my code
NSDate *adate = objIStruct_Conference.m_DtDate;
NSLog(#"DATE========%#",objIStruct_Conference.m_DtDate);
NSString *DateString = [NSString stringWithFormat:#"%#", adate ];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"NEW DATE====%#", [formatter stringFromDate:adate]);
[formatter release];
i am getting DATE===== 2012-01-01 10:00:00 , NEW DATE=====(null)

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"%#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
See the Class Reference of NSDateFormatter

Please use this code:
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",[dateFormat stringFromDate:date]);

it seems your adate pointer would be nil, because it is working perfectly for me on the real device with a valid NSDate but when I set the _date to nil, the output will be nil as in your case.
NSDate *_date = [NSDate date];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[_dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#", [_dateFormatter stringFromDate:_date]);
the output is:
2012-08-08

Related

NSString to NSDate convert

I have a string that contain date:
2012-05-25
and i wan to convert this string to NSDate:
NSString *birthday = /*2012-05-25*/;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-DD"];
NSDate *dateFromString = [dateFormatter dateFromString:birthday];
and in the dateFromString i get the date of today.
This works :
NSString *birthday = #"2012-05-25";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [dateFormatter dateFromString:birthday];
NSLog(#"%#", dateFromString);
Outputs :
2012-01-25 11:36:38.157 dsv[2679:903] 2012-05-25 00:00:00 +0200
Please refer to the Apple guide for details.
I hope this helps u. try this
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}

How to convert NSDate to NSString?

converting NSDate to NSString creates a memory leak can anyone help.
Here is my code:-
NSDate *today = [[NSDate alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
dateString = nil;
dateString =[[NSString alloc]initWithString:[df stringFromDate:today]];
[df setDateFormat:#"EEEE MMM dd yyyy"];
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];
[df release];
[today release];
As you aren't releasing anything the code creates a memory leak.
NSDate *today = [NSDate date]; //Autorelease
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; //Autorelease
[df setDateFormat:#"yyyy-MM-dd"]; // 2017-09-28
dateString = [[df stringFromDate:today] retain];
[df setDateFormat:#"EEEE MMM dd yyyy"]; // Thursday Sep 28 2017
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];
For more details, you can refer to Apple's documentation.
Use
NSDate *today = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
dateString = [df stringFromDate:today];
[df release]
Using your code...
NSDate *today = [[NSDate alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
dateString = nil;
dateString = [[NSString alloc]initWithString:[df stringFromDate:today]];
...you need to release a lot of obj because nothing is in autorelease.
[today release]; -> alloc
[df release] -> alloc
[dateString release]; -> alloc
Or change to:
NSDate *today = [NSDate date];
NSDateFormatter *df = [NSDateFormatter initWithDateFormat:#"yyyy-MM-dd"];
dateString = [df stringFromDate:today];
with no one release/alloc!
The leak is in the DateFormatter that is not being released.
This should fix the leak :
[df release];
Also, try using ...
[NSDate date]
instead of ...
NSDate* today = [[NSDate alloc] init];
that is a lot of alloc/initing that you are doing there as well... you don't need to alloc/init the NSString either.
You Shoud noy alloc or init NSDate object
Try this Code
NSDate *today = [NSDate date];
NSDateFormatter *dt = [[NSDateFormatter alloc]init];
[dt setDateFormat:#"yyyy-mm-dd"];
NSString *str =[NSString stringWithFormat:#"%#",[dt stringFromDate:today]];
NSLog(#"%#",str);
[dt release];
Happy Coding
Yet another response:
NSDateFormatter *df = [NSDateFormatter initWithDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [df stringFromDate:[NSDate date]];
using ARC, all autoreleased.

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 to specify the NSDate Format in Iphone app?

I am trying to make a date format in such a manner hours.minutes day/month/year
I have uses this code :
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSDate *dateAndtime = [NSDate date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
//[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString* dateforBD=[[NSString alloc]init];
dateforBD =[formatter stringFromDate:dateAndtime];
But this code not give me my format.
Also i have tried this code:
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy"
options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
It's also give me different format. How can i achieve the required format means 13.10 21/4/2011
All the formats return in month before date. May i have used wrong way for this ?
Thanks in advance.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
To explore more visit http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope it helps.
for date before month use -
[dateFormat setDateFormat:#"dd/MM/yyyy hh:mm"];
As specified in Apple's NSDateFormatter reference, the NSDateFormatter uses Unicode Date Format Patterns.
According to the unicode patterns, you would need the following pattern to achieve "13.10 21/4/2011":
hh:mm dd/MM/yyyy
So you would use this code:
NSDate *dateAndtime = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm dd/MM/yyyy"];
NSString *newlyFormattedDateString = [dateFormatter stringFromDate:dateAndtime];

03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN)

I am using the below code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDate *today=[NSDate date];
NSString *string = [dateFormatter stringFromDate:today];
NSLog(#"%#",string);
[dateFormatter release];
This will not give me this format 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN), if i convert system date and time so the converted string should have such type of information 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN).? What should i do?
you should set the date format as,
[dateFormat setDateFormat:#"dd/MM/YYYY hh:mm a"];
Try doing this and u will get the format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yy HH:mm"];
NSDate *today=[NSDate date];
NSString *string = [formatter stringFromDate:today];
NSLog(#"%#",string);
[formatter release];
Good Luck!