【问题标题】:Convert A NSString into NSDate to add days to the dynamic date将 NSString 转换为 NSDate 以将天数添加到动态日期
【发布时间】:2013-07-22 10:49:05
【问题描述】:

我从数据库中获取一个 NSString(其中我有一个日期),我需要添加天,例如假设 10//12/15/45/60 天到日期,我该怎么做。

当我尝试使用这段代码时:

NSString *expiryDateStr = [NSString stringWithFormat:@"%@",[[assets valueForKeyPath:@"assetrewards.expirydate"] objectAtIndex:0]];
        NSLog(@" date %@",expiryDateStr);

        //DATE FORMATTER
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM/yyyy"];
        NSDate *_date = [dateFormat dateFromString:expiryDateStr];
        [dateFormat setDateFormat:[NSString stringWithFormat:@"dd MMM yyyy HH:mm"]];
        [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*5]];
        NSLog(@"date format %@",dateFormat);
        NSLog(@"Date: %@", _date);

我得到这样的输出:

date 07/10/2013
date format <NSDateFormatter: 0x9f9d4a0>
Date: 2013-10-06 18:30:00 +0000

日期正在更改,我不知道我哪里出错了,如果那是正确的日期,如何将日期添加到日期。

【问题讨论】:

标签: iphone ios nsdate nsdatecomponents


【解决方案1】:
NSString *expiryDateStr = @"07/10/2013";
NSLog(@" date %@",expiryDateStr);
expiryDateStr=[expiryDateStr stringByAppendingString:@" 00:00:00 +000"];
//DATE FORMATTER
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss Z"];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];
[dateFormat setDateFormat:[NSString stringWithFormat:@"dd MMM yyyy HH:mm"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:_date];
NSInteger day = [components day]+1; // U can add as per your requirement


[components setDay:day];
NSDate *NextDate = [[NSCalendar currentCalendar] dateFromComponents:components];

NSLog(@"Next Date: %@", NextDate);

【讨论】:

  • 谢谢库克,对我帮助很大
  • 确实没有必要将时间附加到日期字符串中。时间 00:00:00 将是默认值。而且,正如我上面所说,第二个 setDateFormat 和 setTimeZone 没有做任何事情(除非它用于转换原始日期,否则不应使用该时区)。
  • 库克帕特尔真的帮了我很多
【解决方案2】:

你可以像下面这样和日期...

如下设置时区...

  [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];


    NSDate *dateAftertwenty = [NSDate dateWithTimeInterval:10*24*60*60 sinceDate:date]; //TimeInterval is of 10 days....you can set any number of days.24 is hours, 60 is minutes and 60 is seconds at last..

让我知道它是否有效!!!

编码愉快!!!!

【讨论】:

  • 第一次我需要将有日期的字符串值转换为 NSDate,这是我无法做到的。
  • 只使用格式[dateFormat setDateFormat:@"MMM/dd/yyyy"]; ,只需检查一下,它是否给出了您想要的正确日期......??
  • 我得到空值
  • 在将它转换为 NSDate 对象时我仍然得到空值
  • 日期格式“dd/MM/yyyy”运行良好,因此“MMM/dd/yyyy”失败也就不足为奇了。
【解决方案3】:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSDate *_date = [dateFormat dateFromString:expiryDateStr];

完全没问题——没问题。 “问题”在于

NSLog(@"Date: %@", _date);

将日期显示为 UTC/GMT,因此(在印度)比“真实”日期早五个半小时。这不是问题——当您将日期格式化为不同的字符串格式(使用日期格式化程序的第二个化身)时,它将正确显示(只要您不为该版本设置时区,如果您没有'不要将它设置为第一个)。

要增加日期,通常最好使用 NSDateComponents(或多或少如 Paras 所示)而不是简单地增加 24 小时,以防日期范围内出现任何类型的“夏令时”过渡。

【讨论】:

    【解决方案4】:

    更新:

    首先setTimeZonelocalTimeZone 如下所示..

    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    

    使用我的波纹管方法将该字符串日期转换为NSDate..

    -(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        [formatter setLocale:[NSLocale currentLocale]];
        [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        NSDate *nowDate = [formatter setDateFormat:dateFormat];// set format here which format in string date
        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
    }
    

    并像下面这样使用它..

    NSDate *startDate = [self convertStringToDate:@"07/10/2013" dateFormate:@"dd/MM/yyyy"]; 
    

    上面设置的日期格式是在字符串中定义的格式

    【讨论】:

    • @iOSDev 你试试看??有什么问题??
    • 首先我想将 NSString 转换为 NSDate。我在那里遇到过一次问题,很明显我可以进行下一步。
    • @iOSDev 我更新了答案老兄试试看,让我知道.. :)
    • @iOSDev -- 您非常成功地将字符串转换为 NSDate。您没有意识到当您 NSLog 一个 NSDate 对象时,您得到的是 UTC,而不是您的本地时区。
    • @HotLicks 是的,对,现在我还添加了这一行 [dateFormat setTimeZone:[NSTimeZone localTimeZone]]; .. :)
    【解决方案5】:

    我已经修改了你的代码,我相信这就是你想要的:

    NSString *expiryDateStr = @"07/10/2013";
    NSLog(@" date %@",expiryDateStr);
    
    //DATE FORMATTER
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *_date = [dateFormat dateFromString:expiryDateStr];
    NSLog(@"date format %@",dateFormat);
    NSLog(@"Date: %@", _date);
    

    输出:
    日期 07/10/2013
    日期格式
    日期:2013-10-07 00:00:00 +0000

    现在更改了代码中的时区:

    NSString *expiryDateStr = @"07/10/2013";
    NSLog(@" date %@",expiryDateStr);
    
    //DATE FORMATTER
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+5:30"]];
    NSDate *_date = [dateFormat dateFromString:expiryDateStr];
    NSLog(@"date format %@",dateFormat);
    NSLog(@"Date: %@", _date);
    

    输出是:

    日期 07/10/2013
    日期格式
    日期:2013-10-06 18:30:00 +0000

    这是你得到的输出。这是因为正在使用您计算机上的时区。

    编辑(添加的天数用days整数表示):

    NSString *expiryDateStr = @"07/10/2013";
    NSLog(@" date %@",expiryDateStr);
    
    //DATE FORMATTER
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    
    
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *_date = [dateFormat dateFromString:expiryDateStr];
    NSInteger days = 30;
    _date = [_date dateByAddingTimeInterval:(60*60*24*days)];
    
    NSLog(@"date format %@",dateFormat);
    NSLog(@"Date: %@", _date);
    

    编辑后代码的输出:

    日期 07/10/2013
    日期格式
    日期:2013-11-06 00:00:00 +0000

    【讨论】:

    • 能否请您添加用于添加日期天数的代码,这将完成答案。
    • 我得到了空值,非常感谢你,还有@Cook patel,答案对我有用。
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2011-04-24
    • 2017-04-23
    • 2021-09-11
    相关资源
    最近更新 更多