在 NSString 中将 NSDate 格式化为这种格式:- 2013 年 1 月 15 日

Format NSDate to this format in NSString:- January 15 2013

目前,我将日期存储在 NSArray 中,因为我必须在自定义下拉视图中加载日期列表,但由于以下问题无法这样做。

这是我尝试格式化 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];
     } 

但是我没有得到像 "January 15 2013"

这样的期望输出

同样在 NSDate 中,我得到类似“0000-12-31 18:06:32 +0000”的信息,谁能告诉我为什么这一年即将到来“0000”

您可以在此处查找格式值: http://waracle.net/iphone-nsdateformatter-date-formatting-table/

在你的情况下,你会想要尝试这种格式:

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

将您的日期格式行替换为此行:

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

请检查代码

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

您没有设置 NSDateComponents 对象的年月:

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

(参见 NSDateComponents class reference 示例)。

另外,你的格式化字符串是错误的。您需要:

  • MMMM 全名月份。
  • yyyy 年。

所以:

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

Here is the reference 关注。

按照下面的 post 进行操作。解释的很详细。

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

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];

也看看这个Date and Time它会对你有很大帮助

您的代码的问题在于您在第 1 天初始化 NSDateComponents,并且由于您没有提供任何年份,因此将年份设为“1”。 因此,为了让您的代码正常工作,您可以将当前日期的年份设置为 NSDateComponents 的年份。

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 已经回答了我的问题并且确实有效。

同时,经过一些研究,我通过以下过程解决了这个问题,并添加了一种替代方法:-

   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];
        
    }