nsdate

NSDate independent of timezone?

牧云@^-^@ 提交于 2019-11-30 08:31:56
问题 I have an app that displays a timetable of certain ferry trips. If I travel to a different timezone - say 4 hours behind, a 10am ferry trip now shows up as 6am? I know this has got to do with how dates are treated based on their timezones, but I can't work out how to change that behaviour. At the moment here's how I am getting the date and displaying it on a UILabel: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm"]; [self.departureTime

Find out if an NSDate is today, yesterday, tomorrow

跟風遠走 提交于 2019-11-30 08:28:19
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? runmad 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 :) I understand that this is pretty old, but I want to bring the answer up to date (as I didn't see anyone else post a more up to date answer). Since iOS 8.0 you

NSDateFormatter with default time zone gives different date than NSDate description method

浪尽此生 提交于 2019-11-30 08:22:34
问题 NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp]; NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init]; [_dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]]; [_dateFormatter setDateFormat:@"dd-MM-yy HH:mm"]; NSString *dateString = [_dateFormatter stringFromDate:date]; NSLog(@"Date: %@ formatted: %@", date, dateString); [_dateFormatter release]; Gives me wrong date formatted: Date: 2013-01-31 18:00:00 +0000 formatted: 31-01-13 19:00 (from timestamp: 1359655200

NSDate for first day of the month

折月煮酒 提交于 2019-11-30 07:58:15
This is quite a simple concept, but as of yet I have been unable to find an elegant (and calendar locale independent) solution. I need to find the first day of the month for an arbitrary NSDate . For example, given an arbitrary NSDate ( arbitraryDate ) another NSDate object will be returned (let's call this firstDayOfMonthDate ) which represents the first day of the month in arbitraryDate . The time component does not matter, as I just need the NSDate object for the first day of the month (although for neatness it would be useful if the time was just zeroed out). Thanks in advance for any

Sorting NSMutableArray By Object's Property

喜你入骨 提交于 2019-11-30 07:45:03
问题 So this is a rather basic question regarding the best way to sort an NSMutableArray of custom objects. I have a an NSMutableArray of custom objects, each object with an NSString and NSDate that go together. I need to sort the array by the newest object (so latest NSDate ), and I'm pretty sure I could simply use NSDate compare: NSDate if this was an array of just NSDate , but since I need all objects to be sorted and not just the date, I'm not sure if I can use that method. In terms of pseudo

How to check if 24 hours have passed in Swift

旧时模样 提交于 2019-11-30 07:33:54
I have to implement in my app where after a user had saved his recording, I will have to determine if 24 hours have passed from the creation date of that recording. So far what I have at the moment is just to determine if current date is not equal to the creation date. I really appreciate anybody's help, thanks in advance. You can use UserDefault to save the date upon creation of the record. The syntax will be UserDefaults.standard.set(Date(), forKey:"creationTime") Whenever you want to check the saved date, retrieve it in this way if let date = UserDefaults.standard.object(forKey:

JSON date to NSDate Issue

我与影子孤独终老i 提交于 2019-11-30 06:59:39
I have a date string in JSON format, it looks like this: 2013-05-22T10:54:40.437 And I'm trying to convert it to NSDate. - (NSString *)dateFromString:(NSString *)datestring{ // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"]; NSDate *date = [dateFormat dateFromString:datestring]; //date is nil here. NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init]; [newDateFormatter setDateFormat:@"MM/dd/yyyy"]; NSString *newString = [newDateFormatter stringFromDate:date]; NSLog(@"Date -- %@

How to get NSDate for 00:00 on the day of [NSDate date]?

一曲冷凌霜 提交于 2019-11-30 06:57:09
问题 I need to get an NSDate object for 00:00(beginning of the day) from [NSDate date], let's say if currently it is 11:30am(returned by [NSDate date]), on 01/06/2012, now I need to have an NSDate for 00:00am on 01/06/2012. I haven't tried this, but what is in my mind is: NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |

Subtracting two NSDate objects [duplicate]

假如想象 提交于 2019-11-30 06:00:57
Possible Duplicate: How to Get time difference in iPhone I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this? I know I need to subtract the current date with the date I get from the feed, but I don´t know how to do it. Ex: Date from feed: Date: 2011-06-10 15:00:00 +0000 Today: Date: 2011-06-10 14:50:00 +0000 I need to display that the difference is ten minutes. Thanks! Create two NSDate objects from the strings using NSDate's -dateWithString: , then get the difference

NSTimer not firing

蓝咒 提交于 2019-11-30 05:59:53
问题 I have an NSTimer that I init with this code: testTimer = [[NSTimer alloc] initWithFireDate:[new objectAtIndex:0] interval:0.0 target:self selector:@selector(works:) userInfo:nil repeats:NO]; [new objectAtIndex:0] is an NSDate in the past. When I start up the app, the timer is getting created, with a fireDate of immediately (since the date is in the past), however it never calls my works method. ( -(void)works:(id)sender ) Anybody know why this is happening? 回答1: You will have to add it to