如何为明天早上 8 点创建日期对象 [英] How to create a date object for tomorrow at 8am

查看:83
本文介绍了如何为明天早上 8 点创建日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常很擅长这个,但我在使用 NSDate 对象时遇到了问题.我需要为明天早上 8 点(相对)设置一个 NSDate 对象.我该怎么做,最简单的方法是什么?

I am normally pretty good with this, but I am having trouble with the NSDate object. I need a NSDate object set for tomorrow at 8am (relatively). How would I do this and what is the simplest method?

推荐答案

这里是如何WWDC 2011 会议 117 - 执行日历计算 教会了我:

Here's how WWDC 2011 session 117 - Performing Calendar Calculations taught me:

NSDate* now = [NSDate date] ;

NSDateComponents* tomorrowComponents = [NSDateComponents new] ;
tomorrowComponents.day = 1 ;
NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDate* tomorrow = [calendar dateByAddingComponents:tomorrowComponents toDate:now options:0] ;

NSDateComponents* tomorrowAt8AMComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:tomorrow] ;
tomorrowAt8AMComponents.hour = 8 ;
NSDate* tomorrowAt8AM = [calendar dateFromComponents:tomorrowAt8AMComponents] ;

可惜 iOS 没有 [NSDate dateWithNaturalLanguageString:@"tomorrow at 8:00 am"].感谢rmaddy指出这一点.

Too bad iOS doesn't have [NSDate dateWithNaturalLanguageString:@"tomorrow at 8:00 am"]. Thanks, rmaddy, for pointing that out.

这篇关于如何为明天早上 8 点创建日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆