更改当前日期

发布于 2024-11-02 00:16:10 字数 46 浏览 1 评论 0原文

我正在 iPhone 开发中使用日期。我需要将日期从当前日期增加 24 小时。

I am working with the date in my iPhone development. I need to increase the date by 24 hours from the current date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无名指的心愿 2024-11-09 00:16:10

您可以使用 dateByAddingTimeInterval

NSDate *currentDate = [NSDate date];
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)]; //one day

You can use dateByAddingTimeInterval

NSDate *currentDate = [NSDate date];
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)]; //one day
青衫负雪 2024-11-09 00:16:10

简单的:

NSDate *tomorrow = [[NSDate date] dateByAddingTimeInterval:86400];

Simple:

NSDate *tomorrow = [[NSDate date] dateByAddingTimeInterval:86400];
青衫儰鉨ミ守葔 2024-11-09 00:16:10

您可以添加 NSTimeInterval。由于这是以秒为单位的双倍,只需添加 24*60*60:

- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds

you can add an NSTimeInterval. Since this is a double in seconds just add 24*60*60:

- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds
悲念泪 2024-11-09 00:16:10

尝试

NSDate *twentyFourHoursLater = [[NSDate alloc] initWithTimeIntervalSinceNow:60 * 60 * 24];

Try

NSDate *twentyFourHoursLater = [[NSDate alloc] initWithTimeIntervalSinceNow:60 * 60 * 24];
来世叙缘 2024-11-09 00:16:10

将下载日期保存在 NSUserdefaults 中,然后检查是否已经过了 24 小时?
如果没有通知用户他/她将不得不等待。

Save the download date in the NSUserdefaults then check if 24 hours have passed?
If not notify the user that he/she will have to wait.

冷情 2024-11-09 00:16:10

将上次下载日期保存在 NSUserDefaults。当您尝试下载时,只需检查该日期即可。

像这样的事情:

- (BOOL)downloadDataForecedUpdate:(BOOL)forced {
    NSDate *lastDownloadDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastDownloadDate"];
    if (forced || !lastDownloadDate || [[NSDate date] timeIntervalSinceDate:lastDownloadDate] > 24 * 60 * 60) {
        // start download
        [NSURLConnection connectionWithRequest:myDownloadRequest delegate:self];
        return YES;
    }
    return NO;
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // process data

    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"lastDownloadDate"];
}

您可以在适当的地方调用 [self downloadDataForecedUpdate:NO] 。在应用程序启动时或在 IBAction 中。使用该方法的返回值来显示下载指示器或警告用户必须等待更多时间。

save the last download date in NSUserDefaults. And when you try to download just check for that date.

something like this:

- (BOOL)downloadDataForecedUpdate:(BOOL)forced {
    NSDate *lastDownloadDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastDownloadDate"];
    if (forced || !lastDownloadDate || [[NSDate date] timeIntervalSinceDate:lastDownloadDate] > 24 * 60 * 60) {
        // start download
        [NSURLConnection connectionWithRequest:myDownloadRequest delegate:self];
        return YES;
    }
    return NO;
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // process data

    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"lastDownloadDate"];
}

you can call [self downloadDataForecedUpdate:NO] whereever it is appropriate. on application launch or in an IBAction. Use the return value of the method to either show a download indicator or a alert that tells the user he has to wait more.

梦境 2024-11-09 00:16:10

根据您真正想要实现的目标,仅在时间间隔中添加 86,400 (或 60 * 60 * 24 )可能不是您想要的。例如,如果用户经历夏令时调整,那么您将关闭一个小时,如果发生在午夜附近,甚至会关闭一天。更好的方法是向 NSCalendar 询问结果,该结果考虑了用户本地时区。

NSDate *start = yourDate;
NSDateComponents *oneDay = [NSDateComponents new];
[oneDay setDay:1];

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *end = [cal dateByAddingComponents:oneDay toDate:start options:0];

Depending on what you are really trying to achieve, just adding 86,400 (or 60 * 60 * 24 ) to your time interval might not be what you want. If for example, the user experiences a daylight-savings adjustment, then you will be off by an hour, or even a day if it happens near midnight. A better method is to ask the NSCalendar for the result, which takes into account the users local time zone.

NSDate *start = yourDate;
NSDateComponents *oneDay = [NSDateComponents new];
[oneDay setDay:1];

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *end = [cal dateByAddingComponents:oneDay toDate:start options:0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文