如何使用 Objective C 获取明天早上 6 点的日期?
你好 这是我的问题。 给定今天的日期,我必须获取明天的日期,但时间应设置为早上 6 点。 我使用 NSDate 和 NSDateComponents。但它总是在早上 6 点的基础上额外增加 5 个小时,并且返回的 NSDate 总是在上午 11 点。我在这里做错了什么? 这是我的代码:
//1. Get today's date
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone defaultTimeZone]];
NSLog(@"1 Today %@", today);
//2. Get tomorrow's date
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: today options:0];
NSLog(@"2 nextDate %@", nextDate);
//3. Get the data components for tomorrow's date to set the time
NSDateComponents *nextDateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:nextDate];
NSInteger theDay = [nextDateComponents day];
NSInteger theMonth = [nextDateComponents month];
NSInteger theYear = [nextDateComponents year];
//4. Build tomorrow's date with time at 6am
NSDateComponents *tomorrowComponents = [[NSDateComponents alloc] init];
[tomorrowComponents setDay:theDay];
[tomorrowComponents setMonth:theMonth];
[tomorrowComponents setYear:theYear];
[tomorrowComponents setHour:6];
NSDate *tomorrow = [gregorian dateFromComponents:tomorrowComponents];
NSLog(@"3 Tomorrow %@", tomorrow);
[gregorian release];
Hi
Here is my problem.
Given today's date, I have to get tomorrow's date but the time should be set to 6am.
I use NSDate and NSDateComponents. But it always adds 5 hours extra to 6am and NSDate returned is always at 11am. What am I doing wrong here?
Here is my code:
//1. Get today's date
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone defaultTimeZone]];
NSLog(@"1 Today %@", today);
//2. Get tomorrow's date
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: today options:0];
NSLog(@"2 nextDate %@", nextDate);
//3. Get the data components for tomorrow's date to set the time
NSDateComponents *nextDateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:nextDate];
NSInteger theDay = [nextDateComponents day];
NSInteger theMonth = [nextDateComponents month];
NSInteger theYear = [nextDateComponents year];
//4. Build tomorrow's date with time at 6am
NSDateComponents *tomorrowComponents = [[NSDateComponents alloc] init];
[tomorrowComponents setDay:theDay];
[tomorrowComponents setMonth:theMonth];
[tomorrowComponents setYear:theYear];
[tomorrowComponents setHour:6];
NSDate *tomorrow = [gregorian dateFromComponents:tomorrowComponents];
NSLog(@"3 Tomorrow %@", tomorrow);
[gregorian release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要格式化要记录的日期,以使用正确的区域设置将其转换为字符串。
You need to format the date you are logging to convert to a string using right locale.
您似乎忘记复制时区。
Looks like you forgot to copy the timezone.
这是一个时区问题。您将本地时间设置为早上 6 点,但返回 GMT。
It's a timezone issue. You're setting the time to 6am locally, but returning GMT.