nsdate

NSDateFormatter returns unexpected results

假如想象 提交于 2019-12-10 06:57:45
问题 Why does the following iOS 4.2 code return two different times? NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [dateFormatter setTimeZone:gmt]; NSString* dateString = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"Date/Time is %@", dateString); NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease]; [inputFormatter

Converting NSString to Date

依然范特西╮ 提交于 2019-12-10 03:24:24
问题 I have two strings: date1 = 3-3-2011; and i want to convert in to 3-March-2011 and display it in label. NSString *myString = 3-3-2011; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"d-MM-YYYY"]; NSDate *yourDate = [dateFormatter dateFromString:myString]; //now format this date to whatever you need… [dateFormatter setDateFormat:@"d-MMM-YYYY"]; NSString *resultString = [dateFormatter stringFromDate:yourDate]; [dateFormatter release]; but yourdate

How to format a date like “5 min”, “1 hour,”, “Yesterday”, “1 Week ago”?

血红的双手。 提交于 2019-12-10 00:20:13
问题 I am trying to get something similar to facebook post date/time. Like 1 min, 1 hour, Yesterday, Week ago etc. This is the is function I am using to get this format: ( Oct-22 17:28 PM ) func convertTime(serverTime: Double) -> String { let serverTime = serverTime let date = Date(timeIntervalSince1970: serverTime) let dateFomatter = DateFormatter() dateFomatter.dateFormat = "MMM-dd HH:mm a" let strDate = dateFomatter.string(from: date) print("This is the post's date: \(strDate)") return strDate

How to manage NSDate for different timezones

孤街醉人 提交于 2019-12-09 22:17:11
问题 I have created a calendar in my app, using the date object this way: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]]; NSInteger day = [weekdayComponents day]; NSInteger month = [weekdayComponents month]; NSInteger year = [weekdayComponents year]; m_dateFormatter.dateFormat = @"dd/MM

Determine the number of months between two dates using Cocoa

左心房为你撑大大i 提交于 2019-12-09 16:28:52
问题 How do I calculate the number of months between two dates using Cocoa? Thanks, Stan 回答1: Look at NSCalendars components:fromDate:toDate:options method. It allows you to subtract dates and then extract the months property value 回答2: NSInteger month = [[[NSCalendar currentCalendar] components: NSCalendarUnitMonth fromDate: yourFirstDate toDate: yourSecondDate options: 0] month]; 回答3: To get an answer that includes the fraction of a month the following can be used: - (NSNumber *

Copying an NSDate (wanting independent objects)

会有一股神秘感。 提交于 2019-12-09 15:54:21
问题 NSDate conforms to NSCopying protocol. According to the documentation for NSCopying protocol: a copy must be a functionally independent object with values identical to the original at the time the copy was made. But, when I do this: NSDate *date1 = [NSDate date]; NSDate *date2 = [date1 copy]; NSLog(@"result: date1 0x%x date2 0x%x", (int)date1, (int)date2); // "result: date1 0x2facb0 date2 0x2facb0" The two objects are identical (same object id). What am I missing? How do I get an independent

2 NSDates that should be equal aren't?

白昼怎懂夜的黑 提交于 2019-12-09 13:53:07
问题 I'm using the JSON library from Stig Brautaset(http://code.google.com/p/json-framework) and I need to serialize an NSDate. I was considering converting it into a string before JSONifying it, however, I ran into this weird behavior: Why aren't these NSDates considered equal? NSDate *d = [[NSDate alloc] init]; NSDate *dd = [NSDate dateWithString:[d description]]; NSLog(@"%@", d); NSLog(@"%@", dd); if( [d isEqualToDate:dd] ){ NSLog(@"Yay!"); } 回答1: When you describe the original date object you

DateTime to NSDate

独自空忆成欢 提交于 2019-12-09 13:23:26
问题 How do I convert string 2010-11-19T20:00:00Z into an NSDate object? I've tried using [dateFormatter setDateFormat:@"yyyy-MM-ddTHH:mm:ssZ"] but it looks like I got the wrong custom format style. PS: I don't care about Timezones. Actually, I want the date to stay in the original (UTC) timezone. 回答1: Try using your format string to convert from an NSDate to a string and see what you get. It's easier debugging in that direction than the other. In this case, looks as though you need to escape the

Unable to Parse Date using NSDateFormatter

落花浮王杯 提交于 2019-12-09 12:58:08
问题 I am fetching a RSS, in which i receive the following Date stamp: 2010-05-10T06:11:14.000Z Now i am using NSDateFormatter to parse this datetime stamp. [parseFormatter setDateFormat:@"yyyy-MM-dTH:m:s.z"]; But its not working fine if just remove the time stamp part it works for the date [parseFormatter setDateFormat:@"yyyy-MM-d"]; But if i add the rest of the stuff it returns nil. Any idea ? Thanks in Advance.... 回答1: You need to parse zero padded values for d, H, m, s You need to escape the

List of all American holidays as NSDates

浪子不回头ぞ 提交于 2019-12-09 12:20:28
问题 I'm seeking a way to get all American holidays as an array of NSDate s. Is there a way to implement that? 回答1: Here you'll find RSS-feeds with the holidays. It doesn't list year, but in the docs you'll find information how to change date ranges. Download it. I would suggest ASIHTTPRequest for that task. parse the RSS-feed. you can do so by normal XML-parsing, or you use a specialized parser. MWFeedParser would be one option. Save the dates. either by using CoreData, or Event Kit Framework 回答2