nsdate

NSDateFormatter dateFromString Always Returns nil

我的梦境 提交于 2019-11-27 23:35:01
I want to apologize ahead of time for asking a repeat question, but none of the other solutions have worked for me yet. Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way. This is the code I'm using to create the date from string: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; [dateFormat setLocale:[NSLocale systemLocale]];

NSDateComponents of an NSDate

自作多情 提交于 2019-11-27 23:01:25
问题 How do I get the NSDateComponents of a single NSDate? I don't want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year of a NSDate? 回答1: There's an example in the Apple docs, Listing 3: Getting a date’s components : NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit)

Convert NSDate to String with a specific timezone in SWIFT

北慕城南 提交于 2019-11-27 22:36:20
问题 In my coredatabase I have an "news" entity with a NSDate attribute. My app is worldwide. News was published 2015-09-04 22:15:54 +0000 French hour (GMT +2) To save the date, I convert it, in UTC format. let mydateFormatter = NSDateFormatter() mydateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+00:00" mydateFormatter.timeZone = NSTimeZone(abbreviation: "UTC") var dateToSaved : NSDate! = mydateFormatter.dateFromString(date) The news is recorded with the date : 2015-09-04 20:15:54 +0000 (UTC)

How to add a time interval to an NSDate?

亡梦爱人 提交于 2019-11-27 22:30:13
I have an NSDate and a duration. I need to get the time after the duration Given: The date is "2010-02-24 12:30:00 -1000" duration is 3600 secs I need to get "2010-02-24 13:30:00 -1000" I thought dateWithTimeIntervalSinceReferenceDate: , would do the trick but I see now that this gives a date offset from 1 Jan 2001 GMT. Is there another C function I need to use As DyingCactus states, you can use the addTimeInterval method of NSDate, but depending on the OS version is will create a compiler warning, since it is deprecated in 10.6 as of 2009-08-17. The current recommended method is to use

How to sort NSMutableArray of date objects

北城以北 提交于 2019-11-27 21:41:18
I'm doing some small work on sorting the date strings in the NSMutableArray, i'm getting the array from the sqlite db. If you print the array it is showing like this date strings are ( "2011-05-01", "2011-02-01", "2012-01-08", "2012-05-08", "2010-01-09 ) I want to show the dates in ascending order. Please help me out guys..... I'm newbie to objc.. Anoop Vaidya First you should convert all date Strings (which is NSString ) objects to NSDate objects and then sort these dateObjects . I believe you have dateArray containing all those strings. NSSortDescriptor *descriptor = [[NSSortDescriptor alloc

Number of days between two NSDate objects

早过忘川 提交于 2019-11-27 21:24:01
问题 I'm trying to compare two days of NSDate objects, I used at the first time NSDateComponents to calculate difference between my two dates Objects, but it is working correctly. NSString *dateStr =@"2012-11-05 15:00:00"; NSDate *date = [dateFormat dateFromString:dateStr]; NSDateComponents *datesDiff = [calendar components: NSDayCalendarUnit fromDate: date toDate: [NSDate date] options: 0]; That does not resolve my problem. Indeed when date1 = 2012-11-05 23:00:00 and date2 = 2012-11-06 10:00:00,

How to find weekday from today's date using NSDate?

廉价感情. 提交于 2019-11-27 20:04:42
I know that I can figure out today's date by [NSDate date]; but how would I find out today day of the week, like Saturday, Friday etc. I know that %w can be used in NSDateFormatter , but I don't know to use it. kennytm NSCalendar* cal = [NSCalendar currentCalendar]; NSDateComponents* comp = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]]; return [comp weekday]; // 1 = Sunday, 2 = Monday, etc. See @HuguesBR 's answer if you just need the weekday without other components ( requires iOS 8+ ). NSInteger weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:

How can i get next date using NSDate? [duplicate]

☆樱花仙子☆ 提交于 2019-11-27 19:05:27
Possible Duplicate: iOS and finding Tomorrow How can i get next date using NSDate. Please send me the solution. In the following, yourDate represents your input NSDate; nextDate represents the next day. // start by retrieving day, weekday, month and year components for yourDate NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate]; NSInteger theDay = [todayComponents day]; NSInteger theMonth = [todayComponents month];

How to convert string to date to string in Swift iOS? [duplicate]

人盡茶涼 提交于 2019-11-27 18:42:08
This question already has an answer here: Converting NSString to NSDate (and back again) 17 answers Am learning swift and am struck in converting the date String to NSDate to string. Am getting the date string in this format "Thu, 22 Oct 2015 07:45:17 +0000". I need to show the date in the MM-dd-yyyy format. I tried the following code but, it returns "null". let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy" dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle let dateObj = dateFormatter.dateFromString(dateString!) print("Dateobj: \(dateObj)") Can anyone please

How to sort an array of dates in descending order

老子叫甜甜 提交于 2019-11-27 17:36:32
问题 I have a NSDictionary that parsed to an array one of the element is date, i tried using [startimeArray sortUsingSelector:@selector(compare:)]; (starttimeArray) is my date but it only arrange ascending. how can i sort in descending order. thanks 回答1: Sort the array by puting NO is ascending parameter: NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO]; NSArray *descriptors=[NSArray arrayWithObject: descriptor]; NSArray *reverseOrder=[dateArray