- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须在本地时间下午 6:00 设置 UIAlertNotification。时间设置为上午 11:00,并将到达晚上 18:00,因为我现在所在的本地时区是 GMT+6:00。但是,在不同的时区,情况会有所不同。我希望任何时区的时间都是下午 6:00。有人可以帮我吗?谢谢。
NSDateComponents *comps=[[[NSDateComponents alloc]init]autorelease];
[comps setYear:[year intValue]];
[comps setMonth:[month intValue]];
[comps setDay:[day intValue]];
[comps setHour:[@"11" intValue]];//6pm
[comps setMinute:0];
[comps setMinute:0];
[comps setTimeZone:[NSTimeZone localTimeZone]];
NSCalendar *cal=[[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date1=[[cal dateFromComponents:comps]retain];
//本地警报代码
NSInteger minutesAhead = 0;
NSTimeInterval seconds = (60 * minutesAhead) * -1;
NSDate *actualFireDate = [date1 dateByAddingTimeInterval:seconds];
NSMutableDictionary *dictC = [NSMutableDictionary dictionaryWithDictionary:userInfo];
[dictC setObject:date1 forKey:@"PCOriginalReminderDate"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = actualFireDate;
notification.alertBody = message;
notification.userInfo = dictC;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
注意事项:
当我打印日期时,它显示:
date = "2012-04-04 18:00:00 +0000";
我认为这意味着上午 11 点是格林尼治标准时间 (GMT),加上 7 个小时(自格林尼治标准时间 +7 - 日光起),就变成了晚上 18 点。
最佳答案
您想要设置 timeZone property您的UILocalNotification
:
The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.
因此,如果您将通知的 fireDate
设置为您在代码片段中创建的时间,并将其 timeZone
设置为 [NSTimeZone localTimeZone]
那么你就可以走了。
关于objective-c - 关于 NSDateComponents 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814526/
我正在创建一个每周日历 View ,其中一部分是计算所显示的一周的第一天。我还希望保存的日期具有第一秒的时间,因此我试图确保日期的 hh:mm:ss 均为 00:00:00。 我有以下代码,在大多数情
我正在编写一些代码来获取服务器上的图像。图像文件名采用 yyyyMMdd_HHmm 格式(UTC),每 3 小时生成一次(1200,1500,1800,..等)。当代码启动时,我获取当前 UTC 日期
当我到达 2011 年 11 月 6 日时,我似乎无法弄清楚为什么这一天没有改变。我所做的只是遍历这些日子。任何帮助将不胜感激。这是我的代码: NSCalendar* calendar = [[NSC
我已经从Zaph阅读以下答案 iPhone - Correct way for getting current date and time for a given place / timezone a
如果您要运行此代码: // January 16th 2015 10:20 AM in Amsterdam var date = NSDate(timeIntervalSince1970: 14214
我有这段代码 NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCa
我正在使用多个 NSDateComponents 来显示一个时钟,其各个值在整个屏幕上被打散,但我遇到了格式化问题。 秒数显示为 1-60 而不是 00-59,小时数为 24 小时制而不是 12 小时
我有以下代码。当我在我的 iOS 3.1.3 设备上运行它时(克服它!)它按预期工作,但是当我在模拟器(iOS 5 和 4.3)上运行它时它会关闭 1 分 15 秒(9:01:15 ).这是因为我的代
我正在尝试获取两个日期之间的差异,它应该以天为单位显示。 NSLog(@"%@", [NSDate date]); NSDateFormatter *formatter = [[NSDateForma
我正在创建一个 NSDate。为什么一天总是少一天? 这是代码: NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar
为什么这给我错误的日期? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCa
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我正在尝试从 2 个变量获取数据:hours = "08" 和 minutes = 30。我是这样做的: let calendar = NSCalendar(identifier: NSCalenda
我的问题是处理下面代码片段中给出的 NSDateComponents 操作的时间配置文件。 我有一个迭代大量 NSDate 对象的方法 该方法的一部分要求我放弃每个日期的小时、分钟和秒数。 为此,我有
我试图在 NSDateComponents 中设置时间,我编写了以下代码 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIden
我不明白为什么我不能使用 NSDateComponents 正确地更改 NSDate 的小时,我会这样做: NSDateComponent components = [calendar compone
一年有多少周?因为我在网上搜索过,我发现一年总共有 52.1 周,但是如果我在 Xcode 中这样做: NSCalendar *gregorian = [[NSCalendar alloc]
我希望显示来自 NSDate 对象的月数。 //Make Date Six Months In The Future NSCalendar *calendar = [[NSCalendar alloc
如果我有两个 nsdatecomponent 并且我想知道第一个对象中的 TIME 是否大于第二个。 例子: if (nsdatecomponentObject1 > nsdatecomponentO
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我是一名优秀的程序员,十分优秀!