nsdate

Objective C - calculating the number of days between two dates

青春壹個敷衍的年華 提交于 2019-11-26 02:34:06
问题 Possible Duplicate: How can I compare two dates, return a number of days. I have two dates (as NSString in the form \"yyyy-mm-dd\"), for example: NSString *start = \"2010-11-01\"; NSString *end = \"2010-12-01\"; I\'d like to implement: - (int)numberOfDaysBetween:(NSString *)startDate and:(NSString *)endDate { } Thanks! 回答1: NSString *start = @"2010-09-01"; NSString *end = @"2010-12-01"; NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@"yyyy-MM-dd"]; NSDate *startDate =

How do I get the day of the week with Foundation?

久未见 提交于 2019-11-26 02:07:17
问题 How do I get the day of the week as a string? 回答1: NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"EEEE"]; NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]); outputs current day of week as a string in locale dependent on current regional settings. To get just a week day number you must use NSCalendar class: NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDate Comparison using Swift

左心房为你撑大大i 提交于 2019-11-26 02:05:51
问题 I am working on an app the requires checking the due date for homework. I want to know if a due date is within the next week, and if it is then perform an action. Most of the documentation I could find is in Objective-C and I can\'t figure out how to do it in Swift. Thanks for the help!! 回答1: I like using extensions to make code more readable. Here are a few NSDate extensions that can help clean your code up and make it easy to understand. I put this in a sharedCode.swift file: extension

Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

心不动则不痛 提交于 2019-11-26 02:00:08
问题 I\'m having trouble converting an ISO 8601 timestamp into an NSDate . I tried to use NSDateFormatter , but I can\'t get it to work with the UTC time offset that appears on the end of the timestamps. To explain, I would like to convert a timestamp such as the following into an NSDate : 2011-03-03T06:00:00-06:00 . My question is: How do I deal with the \"-06:00\" part? I tried using yyyy-MM-dd\'T\'HH:mm:ssZ as my date format string but it doesn\'t work. Any suggestions? 回答1: No need to remove

Converting a string to an NSDate

白昼怎懂夜的黑 提交于 2019-11-26 01:58:39
问题 How is it possible to convert a string to an NSDate on iOS? 回答1: NSString *dateStr = @"20100223"; // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyyMMdd"]; NSDate *date = [dateFormat dateFromString:dateStr]; // Convert date object to desired output format [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"]; dateStr = [dateFormat stringFromDate:date]; [dateFormat release]; Hope this will help you. 回答2: You'll want to take

Sort NSArray of date strings or objects

吃可爱长大的小学妹 提交于 2019-11-26 01:37:28
问题 I have an NSArray that contains date strings (i.e. NSString) like this: \"Thu, 21 May 09 19:10:09 -0700\" I need to sort the NSArray by date. I thought about converting the date string to an NSDate object first, but got stuck there on how to sort by the NSDate object. Thanks. 回答1: Store the dates as NSDate objects in an NS(Mutable)Array, then use -[NSArray sortedArrayUsingSelector: or -[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as the parameter. The -[NSDate compare:]

NSDate() or Date() shows the wrong time

一个人想着一个人 提交于 2019-11-26 01:31:03
问题 When I try to log the current date: print(NSDate()) or print(Date()) (in Swift 3) Or any date object, it shows the wrong time. For example, it\'s about 16:12 now, but the above displayed 2016-10-08 20:11:40 +0000 Is my date in the wrong time zone? How do I fix my date to have the correct time zone? Why is that, and how to I fix it? How do I easily display an arbitrary date in my local time zone, either in print statements or in the debugger? (Note that this question is a \"ringer\" so that I

Convert NSDate to NSString

我的梦境 提交于 2019-11-26 01:29:24
问题 How do I convert, NSDate to NSString so that only the year in @\"yyyy\" format is output to the string? 回答1: How about... NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; //Optionally for time zone conversions [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance]; //unless ARC is active [formatter release]; Swift 4.2 : func stringFromDate(_ date: Date) -> String { let

NSDate get year/month/day

给你一囗甜甜゛ 提交于 2019-11-26 01:10:09
问题 How can I get the year/month/day of a NSDate object, given no other information? I realize that I could probably do this with something similar to this: NSCalendar *cal = [[NSCalendar alloc] init]; NSDateComponents *components = [cal components:0 fromDate:date]; int year = [components year]; int month = [components month]; int day = [components day]; But that seems to be a whole lot of hassle for something as simple as getting a NSDate \'s year/month/day. Is there any other solution? 回答1:

Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?

那年仲夏 提交于 2019-11-26 00:59:23
问题 If I use the following code: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@\"yyyy-MM-dd\'T\'HH:mm\"]; NSDate *myDate = [dateFormatter dateFromString:@\"2010-01-28T15:22:23.863\"]; NSLog(@\"%@\", [dateFormatter stringFromDate:myDate]); It is successfully converted to a Date object, however, I cannot seem to format it any other way than yyyy-MM-dd\'T\'HH:mm , i.e. what gets logged is 2010-01-28T15:22:23 If I change the dateFormat to say