nsdate

NSTimer Pause not working

早过忘川 提交于 2019-12-12 21:45:36
问题 I have setup three buttons with start , stop , pause . and given controls to NSTimer to calculates.start stop button works fine gives me the start and stop time but pause button does not gives me accurate time .it actually pause time ..but start again it adds the paused timed and disp[ay. supoose i pause at 5 second of start and wait for 5 sec then press start...it should display 5 ...but displaying 10.. -(void)start:(NSTimer *)timer { if(_timer==nil) { startDate =[NSDate date]; _timer=

Date formatter for converting 14-sept-2011 in 14th Sept

家住魔仙堡 提交于 2019-12-12 20:26:17
问题 I have a string 14-Sep-2011 In need to convert this 14th Sept. This date may be any date string. Is there any date formatter which allows me to convert date in my format. As if date is 1-Sept-2011 then I need 1st Sept, 2-Sept-2011 should say 2nd Sept. Can anyone please suggest the solution. Thanks 回答1: - (NSString *)ordinalSuffixFromInt:(int)number { NSArray *cSfx = [NSArray arrayWithObjects:@"th", @"st", @"nd", @"rd", @"th", @"th", @"th", @"th", @"th", @"th", nil]; NSString *suffix = @"th";

How to check if NSDate is in current week?

↘锁芯ラ 提交于 2019-12-12 16:00:24
问题 How to check if NSDate falls in current week of the year? I tried below but last week's date is also falling inside "Date is in this week" condition. - (NSInteger)thisW:(NSDate *)date { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *todaysComponents = [gregorian components:NSWeekCalendarUnit fromDate:[NSDate date]]; NSUInteger todaysWeek = [todaysComponents weekday]; NSDateComponents *otherComponents = [gregorian components

Proper way to convert NSString to NSDate on iOS?

☆樱花仙子☆ 提交于 2019-12-12 15:14:43
问题 I've been using this method to convert a regular NSString object to an NSDate, but tried submitting an update to Apple and it was denied. What's another way to do this in iOS? NSString *date_str = @"2011-08-12T12:20:00Z"; NSDate *the_date = [NSDate dateWithNaturalLanguageString:date_str locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; Thanks in advance! 回答1: Use an NSDateFormatter. Brief example follows: ... NSString* dateString = "2011-08-12T12:20:00Z";

NSDate crash with successive actions

拟墨画扇 提交于 2019-12-12 14:23:42
问题 I have the following code below that is meant to change a class var called "today" forward or backward by one day. It will work one time but then after that it crashes. It will do the same no matter if I press the left button or right button. What am I doing wrong? the var today is a class var initiated as .. today = [NSDate date] Here is the method that crashes: (IBAction)changeDateByOne:(id)sender{ NSDate *newDay; NSDate *currentDay = today; NSTimeInterval secondsPerDay = 24 * 60 * 60; if

Changing background according to time - Objective C

て烟熏妆下的殇ゞ 提交于 2019-12-12 13:04:58
问题 So what I'm trying to do is change the background of an image according to the time of day, but it's running into a problem. Here's the code: -(void)updateBackground { NSDate *currentTime = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm a"]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *resultTime = [dateFormatter stringFromDate:currentTime]; [timeLabel setText:resultTime]; NSString *morningStart =

iOS and Objective-C: Repeating an event every quarter

对着背影说爱祢 提交于 2019-12-12 12:10:16
问题 I need to have an event repeat every "quarter" (which I assume means moving up 3 months approximately). So I could expect this to move the date (but it doesn't): NSDateComponents *component = [[NSDateComponents alloc] init]; // quarter component.quarter = 1; self.todoStartDate = [[NSCalendar currentCalendar] dateByAddingComponents: component toDate:self.todoStartDate options: 0]; Is there anything wrong with adding a "quarter" to a date? It works fine when adding a day or a week, but not with

In NSDateComponents, how do you know if it's AM or PM?

坚强是说给别人听的谎言 提交于 2019-12-12 10:36:53
问题 When I use NSDateComponents, how do I know I'm setting a time as Am or Pm? Or is it 24-hour format? Here's the code: NSDate *morningEnd = [NSDate date]; NSDateComponents *components1 = [gregorian components:NSUIntegerMax fromDate:morningEnd]; [components1 setHour:11]; [components1 setMinute:59]; How do I know if I'm setting it as 11:59 AM or 11:59 PM? 回答1: The hour is defined out of 24 hours. 0 - 11 will be AM, 12 - 23 will be PM. Edit: Corrected my range to be 0 - 23 as per the comments

how can we get the number of sundays on an given month ? ( swift )

♀尐吖头ヾ 提交于 2019-12-12 10:26:02
问题 how can we get the number of sundays from an given month ? in average they are 4 but can we get the exact number of sundays in a specify month , i don't know where to start for this so any hint about the logic behind counting the sundays or any thing related to this will be much appreciated and helpful for me , am trying to get the total number of days in a given month and somewhat am able to do that with this code : func getNumberOfDaysInMonth (month : Int , Year : Int) -> Int{ let

NSDate format and value strange behavior

柔情痞子 提交于 2019-12-12 10:23:30
问题 I have the following code: NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [df setTimeZone:[NSTimeZone systemTimeZone]]; [df setFormatterBehavior:NSDateFormatterBehaviorDefault]; myres.start_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]]; NSLog(@"Start date is %@", [NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data