Tag: nsdate

SWIFT:如何将小时添加到NSDate对象

我从string生成一个NSDate对象。 let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT") let stringToDate = dateFormatter.dateFromString(dateFromService) // 2015-07-20 12:00:43 +0000 我从Web服务器获得这个string值。 我需要修改个人设备时区。 想要添加这个stringToDate对象但不能工作 var addHours : Int = 2 // 2 hours will be added var newDate = stringToDate.dateByAddingTimeInterval(addHours)

NSDateFormatter:如何将datestring与'GMT'转换为本地NSDate?

我需要将从服务器检索到的datestring转换为本地NSDate,string如下所示: "Fri, 30 Nov 2012 00:35:18 GMT" 但现在是11月30日星期五上午11点36分,那么如何将string转换为有意义的本地NSDate呢? 我发现: iPhone:NSDate将GMT转换为当地时间 但看起来是相反的。

NSDate,比较两个date

好的,我已经在这里工作了几天了,而且卡住了。 我想要做的是将当前时间与预定时间进行比较。 我想要发生的是,每天在某些时候,我想要某些代码开火,但不是在我指定的时间之前。 所以,基本上如果“当前时间”是相同的或者等于“预定的时间”,那么激发这个代码。 或者激发这一行代码。 听起来很简单,但我比较两次抓取困难。 我已经格式化了一个string到像这样的date NSString* dateString = @"11:05 PM"; NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [firstDateFormatter setDateFormat:@"h:mm a"]; [firstDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSDate* date = [firstDateFormatter dateFromString:dateString]; NSLog(@"date %@",date); 那么我就像这样抓住现在的时间 NSDate *currentTime = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]]; NSDateComponents* comps = [[NSCalendar currentCalendar] components: NSHourCalendarUnit |NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:currentTime]; [[NSCalendar currentCalendar] dateFromComponents:comps]; 那么我如何比较这两个? 我已经尝试了所有我能想到的,请帮助。

如何将秒设置为零的NSDate

我试图从UIDatePicker获取NSDate ,但它不断地返回我一个20秒的date时间。 我如何手动设置NSDate的第二个零?

如何在用户通知中设置重复频率

直到iOS 9我们写这样的local notifications UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = pickerDate; localNotification.alertBody = self.textField.text; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitMinute; localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 并在本地通知我们有repeatInterval ,现在在WWDC2016苹果宣布其中包含User Notification UNTimeIntervalNotificationTrigger。 UNCalendarNotificationTrigger。 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES]; 上面的代码会在每分钟后触发通知。 但是不能设定date。 NSDateComponents* date = [[NSDateComponents alloc] init]; date.hour = 8; date.minute […]

将NSDate转换为毫秒纪元时间

我需要能够将date转换为时间戳,以毫秒为单位的时间。 所有我在网上看到的转换毫秒NSDate而不是相反。 有什么帮助吗?

转换NSString-> NSDate?

我需要转换我的NSString到最后一个NSDate,但我有这样做的问题。 如何使用此代码实现相同的function,同时使最终结果为NSDate? //Date and Format it NSDate *now = [NSDate date]; NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"MM-dd-yyyy"]; NSString *dateString = [formatter stringFromDate:now]; 谢谢! 编辑: //Date and Format it NSDate *now = [NSDate date]; NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"MM-dd-yyyy"]; NSString *dateString = [formatter stringFromDate:now]; NSDate *enddate = [formatter dateFromString:dateString]; […]

NSDate和NSDateComponents返回错误的date

我知道这个问题被问了一百万次,我只是找不到解决办法。 问题是:我正在修改NSDate以返回当前月份的第一天的date或下一个月的第一天的date。 除了1件事外,它似乎工作正常:它将时间减去2或3个小时。 码: NSCalendar *theCalendar = [NSCalendar currentCalendar]; int comp = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *theComp = [theCalendar components:comp fromDate:date]; [theComp setTimeZone:[NSTimeZone systemTimeZone]]; switch (roundingMode) { case FIRST_OF_MONTH: [theComp setDay:1]; return [theCalendar dateFromComponents:theComp]; case FIRST_OF_NEXT_MONTH: [theComp setMonth:theComp.month+1]; [theComp setDay:1]; return [theCalendar dateFromComponents:theComp]; } 这是debugging器(gdb)输出: (gdb) po theComp <NSDateComponents: 0x6e26170> TimeZone: Europe/Kiev (EET) […]

sorting包含NSDate对象的NSArray

我有一个包含NSDate对象的数组。 通过使用Objective-C,我可以使用以下命令对数组进行sorting: NSArray *array = [unsortedArray sortedArrayUsingSelector:@selector(compare:)] 我想知道是否有一个Swift相当于做同样的事情。 在这里,Swift相对比较新,骇人听闻。

Dateformatter给对话时间错误

我想将我的datestring转换为NSDate但它返回正确的date和错误的时间。 这是我的代码: NSString *dateStr = @"2013-12-20 12:10:40"; // Convert string to date object NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr]; NSLog(@"lastUpdatedate : %@",lastUpdatedate); 它返回这个: lastUpdatedate : 2013-12-20 06:40:40 +0000