nsdate

Hardware-dependent NSDateFormatter dateFromString: bug (returns nil)

余生长醉 提交于 2019-11-26 11:35:53
问题 I have several date strings that I need to convert to NSDates. My parsing code is the following: NSString *s = [pair objectForKey:@\"nodeContent\"]; NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@\"yyyy-MM-dd\'T\'HH:mm:ssZZ\"]; self.date = [f dateFromString:s]; The above code works fine in all the devices and simulators we\'ve tested. The strange thing is that when the above code runs on any iPhone 5 running iOS 6.1 or 7.0.x , the line self.date = [f dateFromString:s];

NSDateFormatter return incorrect date from string

我只是一个虾纸丫 提交于 2019-11-26 11:31:25
问题 I have a method, + (NSDate *) convertToDateFrom:(NSString *) dateString { if (dateString == nil || [dateString isEqual:@\"\"]) return nil; //return nil if dateString is empty NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@\"EEEE, dd MMMM yyyy HH:mm\"]; NSDate *date = [df dateFromString:dateString]; return date; } When I pass, @\"Monday, 21 November 2011 17:01\" //Passed string It returns a wrong date, 2011-11-21 23:14:00 +0000 // Output I am not sure whether I am

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

和自甴很熟 提交于 2019-11-26 11:21:56
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. Xcode 8 or later • Swift 3 or later extension Date { func localizedDescription(dateStyle: DateFormatter.Style = .medium, timeStyle: DateFormatter

Objective-C setting NSDate to current UTC

送分小仙女□ 提交于 2019-11-26 10:19:50
问题 Is there an easy way to init an NSDate with the current UTC date/time? 回答1: [NSDate date]; You may want to create a category that does something like this: -(NSString *)getUTCFormateDate:(NSDate *)localDate { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setTimeZone:timeZone]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:localDate];

How to get time (hour, minute, second) in Swift 3 using NSDate?

落花浮王杯 提交于 2019-11-26 09:26:46
问题 How can you determine the hour, minute and second from NSDate class in Swift 3? In Swift 2: let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(.Hour, fromDate: date) let hour = components.hour Swift 3? 回答1: In Swift 3.0 Apple removed 'NS' prefix and made everything simple. Below is the way to get hour, minute and second from 'Date' class (NSDate alternate) let date = Date() let calendar = Calendar.current let hour = calendar.component(.hour,

Convert string to date in Swift

六眼飞鱼酱① 提交于 2019-11-26 09:25:21
问题 How can I convert this string \"2016-04-14T10:44:00+0000\" into an NSDate and keep only the year, month, day, hour? The T in the middle of it really throws off what I am used to when working with dates. 回答1: Convert the ISO8601 string to date let isoDate = "2016-04-14T10:44:00+0000" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX let date = dateFormatter.date

How to determine if an NSDate is today?

风流意气都作罢 提交于 2019-11-26 09:15:25
问题 How to check if an NSDate belongs to today? I used to check it using first 10 characters from [aDate description] . [[aDate description] substringToIndex:10] returns string like \"YYYY-MM-DD\" so I compared the string with the string returned by [[[NSDate date] description] substringToIndex:10] . Is there more fast and/or neat way to check? Thanks. 回答1: In macOS 10.9+ & iOS 8+, there's a method on NSCalendar/Calendar that does exactly this! - (BOOL)isDateInToday:(NSDate *)date So you'd simply

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

冷暖自知 提交于 2019-11-26 08:34:58
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? No need to remove the :'s. To handle the "00:00" style timezone, you just need "ZZZZ": Swift let dateString = "2014-07-06T07:59:00Z"

Convert milliseconds to NSDate

不想你离开。 提交于 2019-11-26 08:29:55
问题 I have this \"1273636800000\" and I want to convert it to this format \"Wed Mar 03 2010 00:00:00 GMT-0500 (EST)\" I need to convert this milliseconds to NSDate format. I tried this NSDate *tr = [NSDate dateWithTimeIntervalSince1970:1273636800000]; and NSDate *tr = [NSDate dateWithTimeIntervalSinceReferenceDate:1273636800000]; and NSDate *tr = [NSDate dateWithTimeIntervalSinceNow:1273636800000]; But I can\'t get it to work, anyone had a similar problem and found the solution 回答1: These

Limiting UIDatePicker dates from a particular time. Such as Input DOB to a restricted age limit

夙愿已清 提交于 2019-11-26 08:29:46
问题 In my code, I have a UITextField that when the user taps on opens a UIDatePicker to enable the user to easily and efficiently scroll through to their Date Of Birth. Obviously, we wouldn\'t want the UIDatePicker to scroll up to 2015 and over as it currently does. As it\'s a Date Of Birth entry field, i would also need to be able to limit entries to 16years+. How do i do this? class SignUpViewController: UIViewController, UITextFieldDelegate { var datePicker:UIDatePicker! @IBOutlet weak var