-1

I only want date like "29-10-2014" with no time so i did this

NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"dd-MM-yyyy"];
NSString *sDate = [df stringFromDate:date];

If I log the sDate I am getting perfect result. But I dont want NSString I want date object, to do that here what I did is

NSDate *theDate = [df dateFromString:sDate];

Now I am getting 2014-10-29 19:00:00 +0000 I only want 29-10-2014.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
S.J
  • 3,063
  • 3
  • 33
  • 66
  • can you tell me why you want something like this? when ever you want date or time from NSDate, you can make a dateformatter and simply use it. – Pawan Rai Oct 30 '14 at 06:46
  • there are related posts on this, e.g.: http://stackoverflow.com/questions/3916392/removing-time-components-from-an-nsdate-object-using-objective-c-cocoa – H K Oct 30 '14 at 06:46

4 Answers4

1

This is because -[NSDate description] returns you full formatted date.

You can swizzle NSDate's - (NSString *)description{} and return something that you want.
Note that this is a very bad practice

#import <objc/runtime.h>

@implementation NSDate (CustomDescription)

+ (void)load
{
    swizzleInstance(@selector(description),
                    @selector(description_m),
                    [UIViewController class],
                    [self class]);
}

static void swizzleInstance(SEL originalSl, SEL swizzledSl, Class originalCl, Class swizzledCl) {
    Method originalMethod = class_getInstanceMethod(originalCl, originalSl);
    Method swizzledMethod = class_getInstanceMethod(swizzledCl, swizzledSl);
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

- (NSString *)description_m
{
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"dd-MM-yyyy"];
    return [df stringFromDate:self];
}
@end
l0gg3r
  • 8,864
  • 3
  • 26
  • 46
0

here is your solution

you are passing string sdata in nsdate,rather pass nsdate object like this

     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-yyyy"];
    NSDate *now = [[NSDate alloc] init];

    NSDate *last=[dateFormat stringFromDate:now];
    NSLog(@"last==%@",last);
0

You will need to use NSCalender class to get year, month and date components. NSDate class will always be represented in GMT.

Use

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
[calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit) fromDate:date];

Check this article too. Use properties to retrieve value from NSDate http://nshipster.com/nsdatecomponents/

Aniket Bochare
  • 427
  • 2
  • 10
0

Here is the simple code

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString* reqDate = [dateFormatter stringFromDate:[[NSDate alloc] init]];
NSLog(@"reqDate: %@",reqDate);//reqDate: 30-10-2014
Teja Kumar Bethina
  • 3,486
  • 26
  • 34
  • I am also doing same but my date object is from array and its not working for me :(. – S.J Oct 30 '14 at 08:18