nsdate

NSCalendar dateFromComponents: vs NSDateComponents date

三世轮回 提交于 2019-12-11 01:37:06
问题 Say I have the following code which initializes an NSDateComponents: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:1987]; [components setMonth:3]; [components setDay:17]; [components setHour:14]; [components setMinute:20]; [components setSecond:0]; And now I want to convert the components to an NSDate. It seems like there are two ways: Method 1: NSDate *date = [calendar dateFromComponents:components];

Compare two NSDates in iPhone

给你一囗甜甜゛ 提交于 2019-12-11 00:53:24
问题 I am new to iPhone developer, i want to compare two Dates, first date will come from Database let say, newDate and second is todays date, if today is greater then newDate then i want to display alert. Here is my code snippet, NSString *newDate = [formatter stringFromDate:st]; NSDate *today=[[NSDate alloc]init]; if ([today compare:newDate] == NSOrderedDescending) { NSLog(@"today is later than newDate"); } else if ([today compare:newDate] == NSOrderedAscending) { NSLog(@"today is earlier than

NSDate is not converting correctly using NSDateFormatter

↘锁芯ラ 提交于 2019-12-11 00:51:47
问题 I have the following NSDateFormatter: NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMM. d, yyyy"]; NSLocale *usLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [dateFormatter setLocale:usLocal]; and I have a while loop which starts at a given start date and increments the date by one until it reaches a specific end date. NSDate *d = start; while(YES){ NSString *td = [dateFormatter stringFromDate:d]; NSLog(@"converted date %@ to %

How to separate the date and time components of NSDate() in Swift?

一曲冷凌霜 提交于 2019-12-10 23:32:08
问题 Would anyone be able to tell me how to separate the date and time components of NSDate() in Swift? I saw a few things about separating the date itself into components (day, month, year), but nothing really about how to get the time only or the day, month, and year only. Basically, I'm trying to filter data on a table by date, but to also display the time the entry was made. I tried using the plain ol' NSDate(), but then the filtering didn't work because filtering by just today's date didn't

NSDateFormatter Trouble?

醉酒当歌 提交于 2019-12-10 23:16:52
问题 I am trying to parse an RSS feed, but the date is hard to parse. The date string is: publishedDate = "2013-01-08T20:09:02.000Z" Here is my code: NSDateFormatter *utc = [[NSDateFormatter alloc] init]; [utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [utc setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSDate *dtUTC = [utc dateFromString: publishedDate]; NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init]; [dfLocal setDateFormat:@"yyyy-MM-dd"]; [dfLocal setTimeZone:[NSTimeZone

NSDate Formatting

早过忘川 提交于 2019-12-10 21:25:45
问题 I have a date string as follows: "Fri, 17 Jun 2011 19:20:51 PDT" which I need to parse into an NSDate format. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"EEE, d MM YYYY HH:mm:ss zzz"]; NSDate *date = [dateFormatter dateFromString:currentPubDate ]; [dateFormatter release]; The code doesn't appear to be working and I am not sure why. I have looked into the issue but searching through forums etc and can't seem to find an answer. I do believe it

Converting time string to Date format iOS

戏子无情 提交于 2019-12-10 19:55:34
问题 I have a string @"21:57" and I want to save it with the same HH:mm format in NSDate. I tried converting it using: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = @"HH:mm"; NSDate *date = [dateFormatter dateFromString:stringToConvertFrom]; but I'm getting 2000-01-01 21:57:00 +0000 instead of 21:57. 来源: https://stackoverflow.com/questions/18724190/converting-time-string-to-date-format-ios

Objective-C/iOS: Get date for specific day in week (sunday)

时间秒杀一切 提交于 2019-12-10 19:48:34
问题 I need to get the date of a specific day (Sunday) in the current week. I use this date to determine if some weekly resetting should occur, and I do some calculations on last reset date is before sundays date etc. This has in principal been ask and answered many times, and my solution has worked for the past 6 months. But today, the 29th of December 2013, it stopped working. The function used at present time: + (NSDate *) getSundaysDate{ NSDate *currentDate = [NSDate date]; NSCalendar

xcode 4.2 location simulator and Time Zone

二次信任 提交于 2019-12-10 18:22:33
问题 I noticed a nifty setting in the xcode simulator while running that I can change the current location and hence simulate location based tests However, when I try to get the date using NSDateFormatter, the local date is still in PST. I am in the Pacific Time Zone NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Jan 1, 2010 [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; // 1:43 PM NSLocale *usLocale = [

How do I know when to release an NSDate object?

限于喜欢 提交于 2019-12-10 18:18:40
问题 Do either or both of these date pointers requires a [release] when I'm done with them. How would i know this? I'm unsure since I'm not doing init explicitly. NSDate *date = [NSDate date]; NSDate *date = [dateWithTimeIntervalSince1970:100000000]; 回答1: Both are autoreleased, that is you don't need to release them yourself. The rule of thumb is that you own an object if you send +alloc or -copy or explicitly retain it: [[SomeClass alloc] init...] [someObject copy] [some object retain] If you own