3

I have been through all the similar questions, unfortunately non could solve my problem so I asked it. I need my function to return an NSDate and only date, but my return value contains timing as well, I have tried the setTimeStyle noStyle and every possible solution I could come up with, here is the code:

-(NSDate*)stringToDate{
    NSString *dateString = @"01-02-2010";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *date;
    date = [dateFormatter dateFromString:dateString];
    NSLog(@"%@",date);
    return date;
} 

The output is : 2010-01-31 16:00:00 +0000

What I want: 2010-01-31

Amar
  • 13,202
  • 7
  • 53
  • 71
Ben
  • 953
  • 12
  • 27
  • An NSDate has to have a time associated with it, because that is always a part of a full date. When you output using an NSDateFormatter, you can set the time style to no style, and then output the NSString given from the NSDateFormatter. – hukir Nov 14 '13 at 04:54
  • @hukir - Correct, as many others said as well there is no way to have an NSDate return type and modified to show date only, so I'll change it to NSString and try to figure out another solution. – Ben Nov 14 '13 at 05:15

6 Answers6

11

This can't work, when you print a NSDate object it will print the ENTIRE date. the way you get a string representation from a date is by using a NSDateFormatter

NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"dd-MM-yyyy";

NSLog(@"%@", [format stringFromDate:[NSDate new]]);

you can put this in a category on NSDate if you so desire.

cream-corn
  • 1,820
  • 14
  • 26
  • Correct, I tried getting NSString and it worked, but I needed an NSDate as return type. So I guess I need to figure out another solution as NSDate can't be trimmed. – Ben Nov 14 '13 at 05:12
  • @Ben Why is this so important to you? Just curious. – Extra Savoir-Faire Nov 14 '13 at 05:13
  • I would also like to know the motivation...Also in theory (not saying you SHOULD do this) but make a category on NSDate override `- (NSString *) description` and make it print out this formatted date string ;) – cream-corn Nov 14 '13 at 05:16
  • @trudyscousin This is a part of a full application, I am getting the parsed data from an XML File, and I want to show the data in a tableview which the date going to be the header for each section. Since there are possibly more than one similar dates in xml parsed data I need to pass up date dynamically to TableView's Sections. Hard to explain unless you see the full application and codes... – Ben Nov 14 '13 at 05:19
  • @Ben, could you post the contents of `- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section` (if you are using it) I would like to see if i can help you solve this problem – cream-corn Nov 14 '13 at 05:32
  • @cream-corn I really really Appreciate if you can help me by suggesting any solution. is there a any other way I can send you the code or communicate directly? Thank you – Ben Nov 14 '13 at 05:45
0

Grab a substring of the first 10 characters. Look at NSMakeRange and subStringWithRange.

NSString* dateOnlyAsString = [[[NSDate date] description] substringWithRange:NSMakeRange(0, 10)];

See the question How to get substring of NSString?.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • This does not answer the question, this is making a substring of a date, what if the date string was '2010-1-1'? – cream-corn Nov 14 '13 at 04:57
  • @cream-corn Look at the last two lines of the question. I answered that. The default output of NSDate's [description](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/description) method is in the style of [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601) (though not exactly). That means the first 10 characters will have the date including padding with leading zeros. To be absolutely sure of that format, the programmer could specify a formatter, as the original poster did indeed. – Basil Bourque Nov 14 '13 at 05:06
0
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDate *now = [[NSDate alloc] init];

NSString *theDate = [dateFormat stringFromDate:now];

NSLog(@"%@",theDate);
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
0

NSDate is always a combination of date and time. You cant change the format of this. But if you want an output mentioned by you, you must convert it into NSString Just add the below code

NSLog(@"%@", [dateFormatter stringFromDate:date]);
manujmv
  • 6,450
  • 1
  • 22
  • 35
0
NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSDate *convertedDate = [df dateFromString:datestring1];
    NSLog(@"convertedDate....%@",convertedDate);
    [df setDateFormat:@"dd-MM-yyyy"];
    NSString     *date1= [df stringFromDate:convertedDate];
Muralikrishna
  • 1,044
  • 10
  • 17
0
    UIDatePicker *datepicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 250, 320, 60)];
    datepicker.datePickerMode = UIDatePickerModeDate;
    NSDate *date = datepicker.date;
    NSDate *date1 = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];

    // convert it to a string
    NSString *dateString = [dateFormat stringFromDate:date1];
    NSLog(@"date %@",dateString);
    datelabel.text =dateString;
    NSLog(@"text in the datelabel.text is %@",datelabel.text);

Try using by the following its works fine.if it not works let me know.