nsdate

NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format

倾然丶 夕夏残阳落幕 提交于 2019-12-18 18:59:11
问题 My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate? I tried: NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30"; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS+05:30"]; NSDate *currentDate = [dateFormatter dateFromString:currentDateString]; NSLog(@"CurrentDate:%@", currentDate); It returns nil. Any thoughts? 回答1: Use this code it is working fine your dateFormatter was wrong . NSDateFormatter *dateFormatter=[

Right way to convert string into NSDate?(iphone)

旧巷老猫 提交于 2019-12-18 18:24:44
问题 In my app I store the date as a string("MM/dd/YYYY" format). In the DB. later when I retrieve the string I have to compare two dates, how do I achieve this? If I had used the"YYYY/MM/dd" format I could have directly compared it as strings. Now I have to convert back into NSDate object using "nsdateformatter" and "dateFromString". But I get a constant date value using this no matter what I do. Any ideas?? 回答1: NSDateFormatter* df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MM/dd/yyyy

Getting Max Date from NSArray, KVC

喜夏-厌秋 提交于 2019-12-18 17:48:33
问题 I've got an array of NSDates and I'd like to grab the largest NSDate from the array. While i could always sort them and grab the first/last, is there a way to do this with KeyValueCoding or some other quick one liner kind of way? I know that I could use something like valueForKeyPath@"@max.date" if the objects had a date property, but what if the objects are dates themselves?? thanks 回答1: You can use, NSDate *maxDate = [dateArray valueForKeyPath:@"@max.self"]; This will give you the largest

How to get number of days between two dates objective-c [duplicate]

别等时光非礼了梦想. 提交于 2019-12-18 17:33:10
问题 This question already has answers here : How can I compare two dates, return a number of days (4 answers) Objective C - calculating the number of days between two dates (6 answers) Closed 6 years ago . I'm trying to make a label that says how many days left till event. I want to calculate the difference between todays date and the events date. I'm using this code and its giving me -4600. It works fine till I use todays date. NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f

NSDate dateFromString deprecated?

南楼画角 提交于 2019-12-18 17:30:08
问题 I'm trying to use the NSDate dateFromString method but I'm getting an warning and it's crashing the app. The code looks like: NSString *pickerDate = [NSString stringWithFormat:@"%@", timeSelector.date]; NSDate *defaultDate = [NSDate dateFromString:pickerDate]; The warning is: 'NSDate' may not respond to '+dateFromString'. It appears that method is deprecated (in the midst of an upgrade from XCode 2 to 3. What alternate method can I use to create a date from a string? 回答1: NSDateFormatter is

sorting an NSArray of NSDates

夙愿已清 提交于 2019-12-18 15:12:09
问题 I have an NSArray of NSDate objects and I want to sort them so that today is at 0, yesterday at 1 etc. Is it ascending or descending, and do i use a function, selector or what? 回答1: There are different sort methods for NSArray because there may be different ways you want to sort things. NSSortDescriptors are a general way that give you a lot of options as far as what keys to use in sorting, what selectors you want to use on those keys, and overall order to use, etc. Or you can use functions

swift - Convert a String to a Date and then to a String in a different format

删除回忆录丶 提交于 2019-12-18 13:31:49
问题 Suppose I have a simple String, extracted from an array of strings (representing dates) - var myDateString = "2015-11-25 04:31:32.0" Now, I want to convert it to a string that looks like Nov 25, 2015 . For that, I assume I need to - get a NSDate date object from string using NSDateFormatter get a string from that NSDate object by using some functionality (which I've been unable to find) I've been trying this in Playground, but have been unable to solve such a simple problem. Here is what I

How to set time on NSDate?

丶灬走出姿态 提交于 2019-12-18 13:17:43
问题 I want to set the NSDate time with my desired hours:minutes:seconds currently im working with NSDate component but it is not giving the desired result [comps setHour: -hours]; [comps setMinute:0]; [comps setSecond:0]; NSDate *minDate = [calendar_c dateFromComponents:comps]; 回答1: Your approach should work fine. I needed a solution for this type problem (setting the individual date components) and the following code works as expected for me. My situation: I wanted to create a date object that

Find out if an NSDate is today, yesterday, tomorrow

人走茶凉 提交于 2019-12-18 13:01:37
问题 With the iOS SDK I need to find an easy and secure way to see if an NSDate is today, yesterday, tomorrow. What I'm looking for is something like this in pseudo code: NSDate *myDate = someDate; if ([myDate isTomorrow]) { NSLog("Tomorrow"); } How would you solve it? 回答1: Check our Erica Sadun's great NSDate extension class: http://github.com/erica/NSDate-Extensions There are lots of date comparisons, among them exactly what you need :) 回答2: I understand that this is pretty old, but I want to

How to change the current day's hours and minutes in Swift?

若如初见. 提交于 2019-12-18 12:05:58
问题 If I create a Date() to get the current date and time, I want to create a new date from that but with different hour, minute, and zero seconds, what's the easiest way to do it using Swift? I've been finding so many examples with 'getting' but not 'setting'. 回答1: Be aware that for locales that uses Daylight Saving Times, some hours may not exist on the clock change days or they may occur twice. Both solutions below return a Date? and use force-unwrapping. You should handle possible nil in your