nsdate

How do I get the current Date in short format in Swift

。_饼干妹妹 提交于 2019-11-26 00:48:35
问题 In the images below you can see the code I wrote and the values of all the variables: class fun getCurrentShortDate() -> String { var todaysDate = NSDate() var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = \"dd-MM-yyyy\" var DateInFormat = dateFormatter.stringFromDate(todaysDate) return DateInFormat } Variable values As you can see the current date is found no problem, but when I try to change the NSDate to a string, it just won\'t do it. 回答1: Xcode 8 or later • Swift 3 or

NSString to NSDate

给你一囗甜甜゛ 提交于 2019-11-26 00:48:14
问题 I got a string that contains the current date by using this : NSString *date = [[NSDate date] description]; At a different point I want to retrieve the date from this string and I used the following code: [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault]; [dateFormatter setDateFormat:@\"YYYY-MM-DD HH:MM:SS ±HHMM\"]; NSDate

How to Check if an NSDate occurs between two other NSDates

北慕城南 提交于 2019-11-26 00:42:59
问题 I am trying to figure out whether or not the current date falls within a date range using NSDate. For example, you can get the current date/time using NSDate: NSDate rightNow = [NSDate date]; I would then like to use that date to check if it is in the range of 9AM - 5PM . 回答1: I came up with a solution. If you have a better solution, feel free to leave it and I will mark it as correct. + (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate { if ([date compare

iOS: Convert UTC NSDate to local Timezone

别说谁变了你拦得住时间么 提交于 2019-11-26 00:36:12
问题 How do I convert a UTC NSDate to local timezone NSDate in Objective C or/and Swift? 回答1: NSTimeInterval seconds; // assume this exists NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds]; NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease]; [df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [df_utc setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"]; NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease]; [df_local setTimeZone:[NSTimeZone

NSDate Format outputting wrong date

允我心安 提交于 2019-11-26 00:29:21
问题 I have a NSString (ex. \"2011-04-12 19:23:39\"), and what I did to format it to a NSDate was the following: [inputFormatter setDateFormat:@\"yyyy-MM-dd HH:mm:ss\"]; NSDate *date = [inputFormatter dateFromString:newDateString]; but what it outputs when I nslog the date is this: 2011-04-12 23:23:39 +0000 which is about 4 hours off. Is there something I missed? Possibly a time zone problem? 回答1: NSDateFormatter use the current device timezone when it created the NSDate object. NSDate stores the

How do I add 1 day to an NSDate?

痞子三分冷 提交于 2019-11-26 00:27:01
问题 Basically, as the title says. I\'m wondering how I could add 1 day to an NSDate . So if it were: 21st February 2011 It would become: 22nd February 2011 Or if it were: 31st December 2011 It would become: 1st January 2012. 回答1: NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; dayComponent.day = 1; NSCalendar *theCalendar = [NSCalendar currentCalendar]; NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0]; NSLog(@"nextDate: %@ ...",

Getting the difference between two NSDates in (months/days/hours/minutes/seconds)

只愿长相守 提交于 2019-11-25 22:35:06
问题 I am trying to get the difference between the current date as NSDate() and a date from a PHP time(); call for example: NSDate(timeIntervalSinceReferenceDate: 1417147270) . How do I go about getting the difference in time between the two dates. I\'d like to have a function that compares the two dates and if(seconds > 60) then it returns minutes, if(minutes > 60) return hours and if(hours > 24) return days and so on. How should I go about this? EDIT: The current accepted answer has done exactly

Converting NSString to NSDate (and back again)

我与影子孤独终老i 提交于 2019-11-25 22:22:58
问题 How would I convert an NSString like \" 01/02/10 \" (meaning 1st February 2010) into an NSDate ? And how could I turn the NSDate back into a string? 回答1: Swift 4 and later Updated: 2018 String to Date var dateString = "02-03-2017" var dateFormatter = DateFormatter() // This is important - we set our input date format to match our input string // if the format doesn't match you'll get nil from your string, so be careful dateFormatter.dateFormat = "dd-MM-yyyy" //`date(from:)` returns an

Difference between 'YYYY' and 'yyyy' in NSDateFormatter

放肆的年华 提交于 2019-11-25 22:11:40
What is exact difference between 'YYYY' and 'yyyy'. I read in this link , it states that A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year. But when I try to use NSString *stringDate = @"Feb 28, 2013 05:30pm"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMM dd, yyyy hh:mma"]; NSDate *date=[dateFormatter

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

旧城冷巷雨未停 提交于 2019-11-25 21:50:44
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 [dateFormatter setDateFormat:@"yyyy-MMMM-d'T'HH:mm"]; the Date object is null... So my ultimate question is how to format