nsdate

how can I add one minutes in current NSDate of iphone and save in NSDate?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 14:39:11
问题 I can get current date of iphone via this code as NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *dateString = [dateFormatter1 stringFromDate:currentDate]; Now I just want to increase one mint then save NSString format, how can I? 回答1: You can use dateByAddingTimeInterval. NSDate *currentDate = [NSDate date]; NSDate *datePlusOneMinute = [currentDate dateByAddingTimeInterval:60]

NSDate from NSString gives null result

我的梦境 提交于 2019-12-01 14:16:01
I am using following code to generate NSDate -> NSString +(NSString *)getCurrentTime { NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString* str =[dateFormatter stringFromDate:now]; [dateFormatter release]; NSLog(@"%@",str); return str; } everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL . Following is my code to retrieve date in specific

number of day of current year in ios

喜你入骨 提交于 2019-12-01 14:07:41
I want to find number of day today is in current year. e.g, if today is Mar15, 2012 I should get 75(31 + 29 + 15). Or we can simply say that number of days between today and Jan01 of current year. can someone please help me? Regards Pankaj Use the ordinalityOfUnit method of the NSCalendar to get the day number in the year - specify the NSDayCalendarUnit inUnit:NSYearCalendarUnit NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDate *today = [NSDate date]; NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:today]; gives 269 for Sept

Comparing time in NSDate

核能气质少年 提交于 2019-12-01 14:00:24
I am trying to compare the time from two NSDate objects but am getting the wrong answer. I think it is because when using the NSDate compare methods it is also including the date. I just want to ignore the date and compare the times. How can this be done? well, you can always use NSCalendar's - (NSDate *)dateFromComponents:(NSDateComponents *)comps 来源: https://stackoverflow.com/questions/3773279/comparing-time-in-nsdate

NSDateFormatter for BST instead of GMT+1

情到浓时终转凉″ 提交于 2019-12-01 13:50:04
I want to display the following string on my time axis: "GMT/BST" Here's the code: NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"zzz"]; timeZoneString = [NSMutableString stringWithFormat:@"%@ / %@",[dateformatter stringFromDate:startDate],[dateformatter stringFromDate:endDate]]; But this gives "GMT/GMT+01:00" What is the NSDateFormatter code to turn "GMT+01:00" into "BST" ? I can't get the right formatters to do this, having tried z|zzz|Z|ZZZ|v|V see... http://waracle.net/iphone-nsdateformatter-date-formatting-table/ Nick T Turns out there is a

init] returning date an hour in the past?

喜夏-厌秋 提交于 2019-12-01 13:34:16
When I call NSDate *now = [[NSDate alloc] init]; in order to get the current date and time, I check it with: NSLog(@"Date now: %@", now); the date outputted is one hour in the past. 2010-10-08 12:04:38.227 MiniBf[1326:207] Now: 2010-10-08 11:04:38 GMT Is my time zone set incorrectly somewhere perhaps? Thanks! Michael Use NSDateFormatter to localize the date: NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]); I've seen that behavior on the DatePicker running on iOS 4.1 devices while on iOS 4.0 and

How to compute age from NSDate

一个人想着一个人 提交于 2019-12-01 13:31:02
问题 I am working on an app that needs to find someones age depending on their birthday. I have a simple NSDate but how would I find this with NSDateFormatter ? 回答1: - (NSInteger)ageFromBirthday:(NSDate *)birthdate { NSDate *today = [NSDate date]; NSDateComponents *ageComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:birthdate toDate:today options:0]; return ageComponents.year; } NSDateFormatter is for formatting dates (duh!). As a rule of thumb, whenever you need

I get nil when using NSDateFormatter in swift

南笙酒味 提交于 2019-12-01 13:28:36
This is my code : println(myStringDate) // 2015-02-26T17:46:34.000Z let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "YYYY-MM-DDTHH:mm:ss.sssZ" let myDate = dateFormatter.dateFromString(myStringDate) println(myDate) // nil Why nil ? What am I doing wrong ? Your problem is how you set the date format. Because there is a T in your string, you need to "escape" it and say, that it shouldn't affect the formatting of the date. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" Also as you see, I've changed the sss to SSS which stands for miliseconds and also changed the year

Problems retrieving NSDate object from Core Data using KVC

为君一笑 提交于 2019-12-01 13:19:46
I have dates stored in core data as NSDate objects. When i try to retrieve them using a fetch request and -(id)valueForKey: i get an integer instead of an NSDate object. NSError *error = nil; NSArray *results = [managedObjectContext executeFetchRequest:request error:&error]; NSManagedObject *entity = [results lastObject]; NSDate *date = [entity valueForKey:@"updated"]; When i use dot notation such as myEntity.updated i get an NSDate object correctly but not when i use a KVC method. The reason I want to use -(id)valueForKey: is because i am running this code on every entity in core data and I

Fire UILocalNotification in a specific Date

末鹿安然 提交于 2019-12-01 12:56:42
I want to fire a UILocalNotification in a specific Date. If I use this code: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components]; If now it's 3 pm this works fine, but it doesn't when, for example, it's 5 pm. How can I set the notification fire date to "the next 4 pm" ? NSCalendar *gregorian = [[NSCalendar