如何使用 Objective C 获取明天早上 6 点的日期?

发布于 2024-10-20 11:13:01 字数 1401 浏览 3 评论 0原文

你好 这是我的问题。 给定今天的日期,我必须获取明天的日期,但时间应设置为早上 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 技术交流群。

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

发布评论

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

评论(3

怂人 2024-10-27 11:13:01

您需要格式化要记录的日期,以使用正确的区域设置将其转换为字符串。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
dateFormatter.dateFormat = @"MM-dd-yyyy h:mm a";
NSLog(@"3 Tomorrow %@", [dateFormatter stringFromDate:tomorrow]);
[dateFormatter release];

You need to format the date you are logging to convert to a string using right locale.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
dateFormatter.dateFormat = @"MM-dd-yyyy h:mm a";
NSLog(@"3 Tomorrow %@", [dateFormatter stringFromDate:tomorrow]);
[dateFormatter release];
爱她像谁 2024-10-27 11:13:01

您似乎忘记复制时区。

Looks like you forgot to copy the timezone.

零時差 2024-10-27 11:13:01

这是一个时区问题。您将本地时间设置为早上 6 点,但返回 GMT。

It's a timezone issue. You're setting the time to 6am locally, but returning GMT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文