gpt4 book ai didi

iphone - 将 NSString 转换为 NSDate 以将天数添加到动态日期

转载 作者:行者123 更新时间:2023-11-28 22:29:38 26 4
gpt4 key购买 nike

我正在从数据库中获取一个 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

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

最佳答案

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

关于iphone - 将 NSString 转换为 NSDate 以将天数添加到动态日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17808968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com