-1

Currently , I am storing the dates in a NSArray, as I have to load the list of dates in custom drop down view, but can't able to do so because of the following issue.

Here is the code how I am trying to format the NSDate:-

 NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSDate *today = [NSDate date];

    NSInteger dc = [currentCalendar  ordinalityOfUnit:NSDayCalendarUnit
                                               inUnit:NSYearCalendarUnit
                                              forDate:today];

    datesArray = [[NSMutableArray alloc]init];
    for (int index = 1; index <= dc; index++)
    {


    NSDateComponents *components = [[NSDateComponents alloc] init];
            [components setDay:index];
    NSCalendar *gregorian = [[NSCalendar alloc]
                                     initWithCalendarIdentifier:NSGregorianCalendar];
            NSDate *date = [gregorian dateFromComponents:components];// Year is coming "0000" --log is: "0000-12-31 18:06:32 +0000"
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"MMMM dd yyyy"]; //updated
            NSString *articleDateString = [formatter stringFromDate:date]; // getting- Jan 01 0001
           [datesArray addObject: pickerItemTitle];
     } 

But I am not getting the desired output like "January 15 2013"

Also in NSDate I am getting something like this "0000-12-31 18:06:32 +0000", can anybody tell me why the year is coming "0000"

Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • Can you try 4M as [formatter setDateFormat:@"MMMM dd YYYY"]; – ondermerol May 20 '15 at 09:03
  • @Larme This is the log of date:- 0000-12-31 18:06:32 +0000 – Vizllx May 20 '15 at 09:07
  • If the year is `0000` how do you expect to have `2013`? – Larme May 20 '15 at 09:08
  • am I doing something wrong, as I am not so good with NSDate. Any suggestion will be helpful @Larme – Vizllx May 20 '15 at 09:09
  • Start with `[components setYears:2013];`? Before your issue with the `NSDateFormatter` (and its `NSString` returns), check how to compose the correct date. – Larme May 20 '15 at 09:10
  • @Vizllx see my answer from this link http://stackoverflow.com/questions/13138957/convert-date-in-mm-dd-yyyy-format-in-xcode/13139015#13139015 and also see all detail documents of NSDate from http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html – Paras Joshi May 20 '15 at 09:11
  • @Larme check my updated question, as [components setYears:2013] will not be helpful as It can be any year. – Vizllx May 20 '15 at 09:11
  • @trojanfoe Currently , I am storing the dates in a NSArray, as I have to load the list of dates in custom drop down view, but can't able to do so because of the following issue. So that's y can set month and year. – Vizllx May 20 '15 at 09:32

8 Answers8

2

You aren't setting the month and year of the NSDateComponents object:

[components setMonth:month];
[components setYear:year];

(see the NSDateComponents class reference for an example).

Also, your formatting string is wrong. You need:

  • MMMM for full name month.
  • yyyy for year.

So:

[formatter setDateFormat:@"MMMM dd yyyy"];

Here is the reference to follow.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Thanks , but I am still not getting the year. just updated the question. check. – Vizllx May 20 '15 at 09:17
  • @Vizllx Because you are still using `YYYY` instead of `yyyy`. See the reference I posted in my answer. – trojanfoe May 20 '15 at 09:18
  • Tested, not working! But when the NSDate is returning like this :- "0000-12-31 18:06:32 +0000" , how can I get proper year, it is coming "0000" – Vizllx May 20 '15 at 09:22
  • @Vizllx Please post your revised code in your question. – trojanfoe May 20 '15 at 09:23
  • my objective is not to set month and years, I am just trying the store date from last 100 years in an Array.So that I can display the list in a drop down view. – Vizllx May 20 '15 at 09:40
  • @Vizllx But you need to set the month and year in the date components object in order to get the correct date object. – trojanfoe May 20 '15 at 09:47
2

The problem with your code is that your are initialising the NSDateComponents with day 1, and since you are not providing any year, it is taking year as "1". So for your code to work properly, you may set the year of the current date as the NSDateComponents' year.

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];

NSInteger dc = [currentCalendar  ordinalityOfUnit:NSCalendarUnitDay
                                           inUnit:NSCalendarUnitYear
                                          forDate:today];

NSMutableArray *datesArray = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{

    //Using dateComponents for setting year
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger year = [dateComponents year];

    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:index];

    [components setYear:year];//Setting year of components

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *date = [gregorian dateFromComponents:components];// Year is coming "0000" --log is: "0000-12-31 18:06:32 +0000"
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MMMM dd yyyy"]; //updated
    NSString *articleDateString = [formatter stringFromDate:date]; // getting- Jan 01 0001
    [datesArray addObject: pickerItemTitle];
    NSLog(@"%@", articleDateString);
}
User31
  • 279
  • 3
  • 11
0

You can look up formatting values here: http://waracle.net/iphone-nsdateformatter-date-formatting-table/

In your case, you will want to try this format:

[formatter setDateFormat:@"MMMM dd YYYY"];
cania
  • 857
  • 10
  • 16
0

Replace your date format line with this line :

[formatter setDateFormat:@"MMMM dd YYYY"];
Utsav Parikh
  • 1,216
  • 7
  • 14
0

Follow this post below. It explains it in detail.

http://b2cloud.com.au/how-to-guides/nsdateformatter-format-strings/

nswamy
  • 1,041
  • 9
  • 16
0
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];

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

NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];

NSLog(@"\n"
      "theDate: |%@| \n"
      "theTime: |%@| \n"
      , theDate, theTime);

[dateFormat release];
[timeFormat release];
[now release];

Also take a look at this Date and Time it will help you alot

Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62
0

User31 has already answered my question and it does work.

Meanwhile after some researching I solved the issue by the following process and added an alternative way of approaching:-

   datesArray = [[NSMutableArray alloc]init];
    
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:1];
    [comps setMonth:1];
    [comps setYear:1900];
    NSDate *minDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
    
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *days = [[NSDateComponents alloc] init];
    NSInteger dayCount = 0;
    while ( TRUE ) {
        
        [days setDay: ++dayCount];
        NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: minDate options: 0];
        if ( [date compare: [NSDate date]] == NSOrderedDescending )
        {
            [self removeLoader];
            break;
        }
        //NSString *articleDateString = [formatter stringFromDate:date];
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        
        [df setDateFormat:@"dd"];
        NSString *dd = [df stringFromDate:date];
        
        [df setDateFormat:@"MMMM"];
        NSString *mm = [df stringFromDate:date];
        
        [df setDateFormat:@"yyyy"];
        NSString *yy = [df stringFromDate:date];
        
        
        NSString *pickerItemTitle = [NSString stringWithFormat: @"%@     %@     %@", mm,dd,yy];
        [datesArray addObject: pickerItemTitle];
        
    }
Community
  • 1
  • 1
Vizllx
  • 9,135
  • 1
  • 41
  • 79
-1

Please Check the Code

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"MMMM dd yyyy";
NSDate *yourDate = [dateFormatter dateFromString:@"January 15 2013"];
Searching
  • 98
  • 1
  • 8