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

时间:2015-05-20 08:59:24

标签: ios objective-c iphone nsdate nsdateformatter

目前,我将日期存储在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];
     } 

但我没有像“2013年1月15日”那样得到所需的输出

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

8 个答案:

答案 0 :(得分:2)

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

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

(有关示例,请参阅NSDateComponents class reference。)

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

  • MMMM全名月。
  • yyyy一年。

所以:

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

Here is the reference要关注。

答案 1 :(得分:2)

您的代码的问题在于您在第1天初始化NSDateComponents,并且因为您没有提供任何一年,所以它将花费一年时间作为&#34; 1&#34;。 因此,为了使代码正常工作,您可以将当前日期的年份设置为NSDateComponents&#39;年。

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

答案 2 :(得分:0)

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

在您的情况下,您需要尝试以下格式:

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

答案 3 :(得分:0)

将此日期格式行替换为以下行:

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

答案 4 :(得分:0)

请按照以下帖子进行操作。它详细解释了它。

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

答案 5 :(得分: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];

另外看看这个Date and Time它会帮助你很多

答案 6 :(得分:0)

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

    }

答案 7 :(得分:-1)

请检查代码

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