Reputation: 652
I am working with NSDate i created one function which give Today date with "dd/MM/yyyy" format but NSDate always show "nil" value. I googled lot and also see lots of stackoverflow answer but all answer return NSString value not NSDate value so please guide me to get NSDate with given format.
+(NSDate*)getCurrentDate{
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
//Date print here
NSLog(@"Utility CurrentDate: %@",[DateFormatter stringFromDate:[NSDate date]]);
NSString *dateString = [DateFormatter stringFromDate:[NSDate date]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:DATE_FORMAT];
//Date print here
NSLog(@"New Date: %@",[newDateFormatter stringFromDate:[NSDate date]]);
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
return dateFromString;
}
Upvotes: 0
Views: 1814
Reputation: 15035
Refer the below modified method in dd/MM/yyyy format
:-
+(NSString*)getCurrentDate{
NSDateFormatter *format=[[NSDateFormatter alloc]init];
[format setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *currentDt=[format stringFromDate:[NSDate date]];
NSDate *dt=[format dateFromString:currentDt];
[format setDateFormat:@"dd/MM/yyy"];
return [format stringFromDate:dt];
}
Upvotes: 0
Reputation: 46563
The issue is here
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
Here your dateString
is 2014-12-11 03:41:31
, hence it is not getting formatted. You should change either newDateFormatter
or DateFormatter
to convert as per your requirement.
EDIT:
Replace [DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
with [DateFormatter setDateFormat: DATE_FORMAT];
But your return date will not be in the desired way, since you are returning an NSDate
Object. You should return a formatted string.
Upvotes: 3
Reputation: 52622
You are completely misunderstanding what NSDate is and does. An NSDate is an absolute point in time. It has no format. It cannot have a format. It is absolutely impossible to have an NSDate in "dd/MM/yyyy" format or in any other format.
When you want to display a date to the user, you use NSDateFormatter to convert the NSDate into a string. In any format you like.
Upvotes: 2
Reputation: 344
check here Convert date from string or you can use this methods to convert.
These all three functions you can use to format date values..
-(NSString*)getStringFromDate:(NSDate*)pDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter stringFromDate:pDate];}
-(NSDate*)getDateFromString:(NSString*)pStrDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter dateFromString:pStrDate];}
-(NSString*)dateStringFromString:(NSString *)dateToConver format:(NSString *)fromFormat toFormat:(NSString *)toFormat{
NSDate *date = [Helper getDateFromString:dateToConver withFormat:fromFormat];
return [Helper getStringFromDate:date withFormat:toFormat];}
Upvotes: 1