nsdate

How to set repeat frequency in User Notification [duplicate]

你离开我真会死。 提交于 2019-11-28 21:39:43
This question already has an answer here: How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications? 3 answers Till iOS 9 we write local notifications like this 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

Convert a NSDate to milliseconds epoch time

此生再无相见时 提交于 2019-11-28 21:17:58
I need to be able to convert a date to a time stamp, an epoch in milliseconds. All I see online are for converting milliseconds to NSDate and not the other way round. Any help out there? NSTimeInterval is a double that already contains sub-second data after the decimal point. Depending what you need, your conversion could be a simple as multiplying by 1000. - (void)testDateFormat { NSDate *date = [NSDate date]; NSLog(@"Time: %f", floor([date timeIntervalSince1970] * 1000)); NSLog(@"Time: %f", floor([date timeIntervalSince1970])); NSLog(@"Time: %lli", [@(floor([date timeIntervalSince1970] *

Check if date is before current date (Swift)

 ̄綄美尐妖づ 提交于 2019-11-28 21:06:38
I would like to check if a NSDate is before (in the past) by comparing it to the current date. How would I do this? Thanks I find the earlierDate method. if date1.earlierDate(date2).isEqualToDate(date1) { print("date1 is earlier than date2") } You also have the laterDate method. Swift 3 to swift 5: if date1 < date2 { print("date1 is earlier than date2") } There is a simple way to do that. (Swift 3 is even more simple, check at end of answer) Swift code: if myDate.timeIntervalSinceNow.isSignMinus { //myDate is earlier than Now (date and time) } else { //myDate is equal or after than Now (date

get time and date by NSDate

笑着哭i 提交于 2019-11-28 19:51:43
问题 how can I retrieve the date and time from a NSDate. I want to see them separately. thanks 回答1: NSDate *localDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = @"MM/dd/yy"; NSString *dateString = [dateFormatter stringFromDate: localDate]; NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init]; timeFormatter.dateFormat = @"HH:mm:ss"; NSString *dateString = [timeFormatter stringFromDate: localDate]; There are many many

Core Data Save Error (NSValidationErrorKey, Cocoa error 1570) saving NSDate

旧巷老猫 提交于 2019-11-28 19:05:14
I'm getting an error with saving to a Core data object in Xcode. Xcode says that the error is in the NSDate variable 'datum' but I have tried almost everything. Error is: 2011-07-12 18:01:29.068 WeekLijstje[3205:207] Core Data Save Error NSValidationErrorKey datum NSValidationErrorPredicate (null) NSValidationErrorObject <DagLijst: 0x6e2fcd0> (entity: DagLijst; id: 0x6e2fd30 <x-coredata:///DagLijst/t99F423FC-AAE9-4692-9264-EF0FF7A020572> ; data: { Voedsel = nil; datum = nil; hoeveelheid = 0; punten = 0; }) NSLocalizedDescription:The operation couldn’t be completed. (Cocoa error 1570.) A small

GMT time on iPhone

最后都变了- 提交于 2019-11-28 18:59:52
How do I get GMT time? NSDate *c =[NSDate date]; gives system time, not GMT. - (NSDate*) convertToUTC:(NSDate*)sourceDate { NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone]; NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate]; NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate]; NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset; NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease]; return destinationDate;

Date/Time parsing in iOS: how to deal (or not deal) with timezones?

五迷三道 提交于 2019-11-28 18:51:24
I have an ASP.NET MVC 3 website that communicates with my iOS app via JSON. As part of the objects sent in the JSON response, I have dates in the format of yyyy-MM-dd HH:mm:ss ZZZ which outputs 2011-04-05 16:28:22 -07:00 . How do I parse that in iOS? This is the code I'm messing around with right now: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; [dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; NSDate *date = [dateFormatter dateFromString:@"2011-04-05T16:28:22-0700"]; NSLog(@"%@; %@; %@", dateFormatter, date,

Swift 3 - find number of calendar days between two dates

允我心安 提交于 2019-11-28 18:26:27
问题 The way I did this in Swift 2.3 was: let currentDate = NSDate() let currentCalendar = NSCalendar.currentCalendar() var startDate : NSDate? var endDate : NSDate? // The following two lines set the `startDate` and `endDate` to the start of the day currentCalendar.rangeOfUnit(.Day, startDate: &startDate, interval: nil, forDate: currentDate) currentCalendar.rangeOfUnit(.Day, startDate: &endDate, interval: nil, forDate: self) let intervalComps = currentCalendar.components([.Day], fromDate:

How to get the hour of the day with Swift?

跟風遠走 提交于 2019-11-28 17:10:54
How would I get the hour of the day in Swift. I have tried NSCalendar and NSDateComponents , but I'm afraid I'm just starting with Swift. Swift 5.0 / 4.0 / 3.0 let hour = Calendar.current.component(.hour, from: Date()) Or, if you're interested in 12 hour AM/PM date format, then use NSDateFormatter let formatter = DateFormatter() formatter.dateFormat = "hh a" // "a" prints "pm" or "am" let hourString = formatter.string(from: Date()) // "12 AM" If you want minutes, seconds and others, do as following let date = Date() // save date, so all components use the same date let calendar = Calendar

iPhone OS: How do I create an NSDate for a specific date?

核能气质少年 提交于 2019-11-28 15:46:06
Seems like a simple thing but I can't seem to find a way to do it. It would be great to see a couple different methods. Oded Ben Dov @Chuck's answer is correct, and lead me to the following code. Thought I'd share: NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:10]; [comps setMonth:10]; [comps setYear:2010]; NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps]; You can use this NSString *dateString = @"03-Sep-11"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"dd-MMM-yy"; NSDate *date = [dateFormatter