nsdate

How to add hours to an NSDate?

戏子无情 提交于 2019-11-28 15:38:49
问题 I have a date converted to double value and saved in database. Now, I want to compare if currentDate > myDataBaseDate + 8 hours i.e., I want to get 8 hours added to myDataBaseDate. I'm converting date into double values. So how do I get 8 hours later time from my database saved date. How do I compare if (currentDateTime > DateFromdatabaseValue + DateByAdding8HoursInDataBase) 回答1: I'm not entirely sure what you are trying to do, but you can create an NSDate object by adding time in seconds on

Bad converting String to NSDate

余生长醉 提交于 2019-11-28 14:51:10
I have a problem with converting String to NSDate. In my text field when user tap on it, is a possibility to chose a date using UIDatePicker . String as a date in TextField looks like that: "yyyy-mm-dd" Now when I try to convert date for example on 2016-05-13 using this method: func changeStringToNSDate(){ let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-mm-dd" let from = moreSorts.fromDatePickerTextField.text! let fromDate = dateFormatter.dateFromString(from) print(fromDate) } When I print fromDate I recieve an information that fromDate is not 2016-05-13 but 2016-01-11

Convert NSString->NSDate?

微笑、不失礼 提交于 2019-11-28 14:45:06
I need to convert my NSString in the end to an NSDate but I am having problems doing so. How would I achieve the same functionality with this code while making the end result an 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]; Thanks! Edit: //Date and Format it NSDate *now = [NSDate date]; NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"MM-dd-yyyy"]; NSString

Converting String to NSDate Giving Wrong Date

你。 提交于 2019-11-28 14:40:30
I have a String that I converted using stringFromDate and now I'm trying to convert it back, however when the UIPicker starts, it's giving me the wrong day let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd MMM YYYY" print(birthday) // logs 15 Jan 1992 let date = dateFormatter.dateFromString(birthday) self.datePicker.setDate(date!, animated: true) I tried hardcoding "15 Feb 1992" but still the same result. The date on UIDatePicker shows 22 Dec 1991 on Start. If I use hardcore 10 Jan 1980 , it starts from 23 December 1979. (I don't know if that's the case but I have MMM dd

Sorting NSArray containing NSDate objects

穿精又带淫゛_ 提交于 2019-11-28 13:57:34
I have an array containing NSDate objects. With Objective-C, I am able to sort this array by using: NSArray *array = [unsortedArray sortedArrayUsingSelector:@selector(compare:)] I'm wondering if there's a Swift equivalent of doing this same thing. Relatively new to Swift here, hacking my way through. Using the native Array type in Swift. If you are interfacing with legacy code from ObjC, use this: let array = unsortedArray.sortedArrayUsingSelector("compare:") If you use Swift array. let datesArray = [ NSDate(), NSDate(timeIntervalSinceNow: 60), NSDate(timeIntervalSinceNow: -60) ] let sorter:

Get current NSDate in timestamp format

六月ゝ 毕业季﹏ 提交于 2019-11-28 13:49:42
问题 I have a basic method which gets the current time and sets it in a string. However, how can I get it to format the current date & time in a UNIX since-1970 timestamp format? Here is my code: NSDate *currentTime = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh-mm"]; NSString *resultString = [dateFormatter stringFromDate: currentTime]; Is it possible to use NSDateFormatter to change the 'resultString' into a timestamp? 回答1: Here

Swift converting NSDate to 24h Format fails on Device

末鹿安然 提交于 2019-11-28 13:13:42
I'm facing an annoying error when i run my application on my iPhone6. I need to convert an hour (picked by a datePicker) from 12h to 24h format. Simple thing! Right, I'm pretty sure about that but... my code works on simulator but not on device. The code is the following: func convertTimeTo24HoursFomratString() -> String { let dateFormatter = NSDateFormatter() // isFormatTime24Hours is a static function that returns me if the device has a 24h clock or not. It works fine both on device and on simulator //if NSDateUtility.isFormatTime24Hours() { // dateFormatter.dateFormat = "h:mm a" // return

Dateformatter gives wrong time on conversation [duplicate]

安稳与你 提交于 2019-11-28 13:09:36
This question already has an answer here: NSDate Format outputting wrong date 5 answers Getting date from [NSDate date] off by a few hours 3 answers I am trying to convert my date string to NSDate but its return correct date and wrong time. This is my code : 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); It returns this : lastUpdatedate

Check whether time falls between two time iOS

可紊 提交于 2019-11-28 13:05:39
I have two times let say 8.40am and 4.00pm, What i want to do is, want to check whether current time falls between given time or not ? I have tried this code snippet but it is not working :( can you please help me out where i am making mistake ? NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; NSInteger currHr = [components hour]; NSInteger currtMin = [components minute]; NSString startTime = @"21:00"; NSString endTime = @"07:00"; NSArray *arr=[NSArray arrayWithObjects:startTime

Error: Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int'

这一生的挚爱 提交于 2019-11-28 13:04:36
问题 I am getting a error named: Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int' on line: if differenceOfDate.second <= 0 Can someone please help! let fromDate = Date(timeIntervalSince1970: TimeInterval(truncating: posts[indexPath.row].postDate)) let toDate = Date() var calendar = Calendar.current let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth, .month]) let differenceOfDate = Calendar.current.dateComponents(components, from: