nsdate

Compare two dates [duplicate]

送分小仙女□ 提交于 2019-12-18 11:11:35
问题 This question already has answers here : How to compare two dates in Objective-C (14 answers) Closed 6 years ago . How can I compare two different dates to find out which is the later date? For example, in date1 I will store one date after downloading some data, and in date2 , I will store the current date. Then I need to check which one is greater/later: something like if(date1>date2) . 回答1: Something like: NSDate* timeNow = [NSDate date]; // If less than 30 seconds, do something if (

How to add one month to an NSDate?

三世轮回 提交于 2019-12-18 10:18:21
问题 How To Add Month To NSDate Object? NSDate *someDate = [NSDate Date] + 30Days.....; 回答1: You need to use NSDateComponents : NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setMonth:1]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:originalDate options:0]; [dateComponents release]; // If ARC is not used, release the date components 回答2: With iOS 8 and OS X 10.9 you can add

Swift: can't get NSDate dateFormatter right

[亡魂溺海] 提交于 2019-12-18 09:46:42
问题 DateFormatter is acting funny to me: let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM'/'DD'/'YYYY" var dateString = "09/28/1989" var date = dateFormatter.dateFromString(dateString) println(date) Returns: "Optional(1988-12-24 13:00:00 +0000)" Any kind of help will be appreciated. 回答1: It looks like your date formatter is a bit off. "MM'/'DD'/'YYYY" reads "Padded month / day of the year (not month) / year of the current week (so Jan 1-6 could overlap and add side-effects)" I

Convert NSDate to an Integer

送分小仙女□ 提交于 2019-12-18 09:23:47
问题 So my dilemma is that I can't figure out how to store the date only in an integer value (fetched using: NSDate* date = [NSDate date] I found this code online which seems similar to what I need (I thought that changing setDateFormat to @"dd" would've worked (but apparently it didn't) NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; //Optionally for time zone conversions [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; NSString

NSDate date method returns wrong result

你说的曾经没有我的故事 提交于 2019-12-18 08:09:44
问题 I know there is a lot of questions of this type, but I did't find solution for my case; I need to get current and correct NSDate object, not NSString ! This code returns wrong time (+3 hours), because I'm from Ukraine. NSDate *currentDate = [NSDate date]; How to get current NSDate object from NSDateFormatter ? I know only how to get NSString , but I don't need it. EDIT: I'm using this code to compare 2 NSDates using NSCalendar object, here is code: NSCalendar *calendar = [NSCalendar

NSDateFormatter not giving me correct

左心房为你撑大大i 提交于 2019-12-18 07:21:26
问题 I am displaying time. It will show me :TIME :2012-06-18 23:00:00 +0000 But after using NSDateFormatter I do not know why it is giving me 00:00:00 AM NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm:ss a"]; NSLog(@"TIME :%@",self.startDate); NSDate *date = [NSDate date]; NSString * _startTime = [dateFormatter stringFromDate:date]; NSLog(@"current time : %@",_startTime); NSString * _startTime1 = [dateFormatter stringFromDate:self.startDate];

Nsdateformatter + Timezone issue

五迷三道 提交于 2019-12-18 07:18:23
问题 I am trying to parse this date timestamp Begin to string: Wed, 11 Sep 2013 08:51:41 EEST This issue with only EEST , I have tried z or zzz or V, nothing happened. Date formatter always getting NULL. While I am cutting EEST from string, everything goes OK. Could anyone suggest, how to solve this issue? Thanks UPDATE: dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_EN_POSIX"]]; [dateFormat setTimeZone:[NSTimeZone

Nsdateformatter + Timezone issue

喜夏-厌秋 提交于 2019-12-18 07:18:08
问题 I am trying to parse this date timestamp Begin to string: Wed, 11 Sep 2013 08:51:41 EEST This issue with only EEST , I have tried z or zzz or V, nothing happened. Date formatter always getting NULL. While I am cutting EEST from string, everything goes OK. Could anyone suggest, how to solve this issue? Thanks UPDATE: dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_EN_POSIX"]]; [dateFormat setTimeZone:[NSTimeZone

NSCalendar, why does setting the firstWeekday doesn't effect calculation outcome?

心已入冬 提交于 2019-12-18 06:06:24
问题 i need to calculate the weekday for a given date, however, depending on the calendar a week can star on Monday and somwhere on Sunday so i wanted to set it, to start on Monday, using [[NSCalendar currentCalendar] setFirstWeekday:2]; however, the calculation outcome is the same { [[NSCalendar currentCalendar] setFirstWeekday:1]; NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date]; NSInteger weekday =

iPhone - get number of days between two dates

自作多情 提交于 2019-12-18 05:03:34
问题 I'm writing a GTD app for the iPhone. For the due tasks, I want to display something like "Due tomorrow" or "Due yesterday" or "Due July 18th". Obviously, I need to display "Tomorrow" even if the task is less than 24 hours away (e.g. the user checks at 11pm on Saturday and sees there's a task on Sunday at 8am). So, I wrote a method to get the number of days in between two dates. Here's the code... NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@