nsdate

How to get local time on iOS [duplicate]

☆樱花仙子☆ 提交于 2020-01-01 03:19:08
问题 This question already has answers here : Retrieving current local time on iPhone? (7 answers) Closed 6 years ago . I just noticed that NSDate *nowDate = [NSDate date]; gives me GMT+0 Time and not the local time. So basically on my iPad it's 13:00 and the output of this code is 12:00. How do I get local time properly? 回答1: Give it a Shot ! NSDate* sourceDate = [NSDate date]; NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSTimeZone* destinationTimeZone = [NSTimeZone

When does dateByAddingComponents:toDate:options return nil?

偶尔善良 提交于 2020-01-01 02:51:08
问题 I've been using dateByAddingComponents:toDate:options: and dateByAddingUnit:value:toDate:options: and using optional binding to get the date out of it. Like this: guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent, toDate: startOfThisMonth, options: []) else { return nil } Where oneMonthComponent is just an NSDateComponent with the month value set to 1. When I read about it, in the documentation they both say something like: Returns nil if date falls outside the

How to check whether now date is during 9:00-18:00

我怕爱的太早我们不能终老 提交于 2019-12-31 21:21:20
问题 When my app is launched I want to check whether the date is between 9:00-18:00. And I can get the time of now using NSDate . How can I check the time? 回答1: So many answers and so many flaws... You can use NSDateFormatter in order to get an user-friendly string from a date. But it is a very bad idea to use that string for date comparisons! Please ignore any answer to your question that involves using strings... If you want to get information about a date's year, month, day, hour, minute, etc.,

How to check whether now date is during 9:00-18:00

感情迁移 提交于 2019-12-31 21:21:05
问题 When my app is launched I want to check whether the date is between 9:00-18:00. And I can get the time of now using NSDate . How can I check the time? 回答1: So many answers and so many flaws... You can use NSDateFormatter in order to get an user-friendly string from a date. But it is a very bad idea to use that string for date comparisons! Please ignore any answer to your question that involves using strings... If you want to get information about a date's year, month, day, hour, minute, etc.,

Calendar.current.date(bySettingHour takes so long to compile

China☆狼群 提交于 2019-12-31 08:00:07
问题 I have used Calendar.current.date(bySettingHour code for setting particular date. Problem is it takes so long to compile ~4 seconds print("Time seconds ",Date().timeIntervalSince1970) for i in 0..<9999 { let nowDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())! } print("Time seconds ",Date().timeIntervalSince1970)// For loop took 4 seconds Is there any way to reduce compile time ? 回答1: You can't test performance in a Playground, and most of all not compile

Convert date format in returns wrong date xcode [duplicate]

限于喜欢 提交于 2019-12-31 03:57:09
问题 This question already has an answer here : NSDateFormatter show wrong year (1 answer) Closed 2 years ago . I want to convert date from 23 May, 2017 to 23-05-2017 . I have tried with the following code but it returns 25-12-2016 . NSDateFormatter *oldFormatter = [NSDateFormatter new]; [oldFormatter setDateFormat:@"dd MMM, YYYY"]; NSDate *oldDate = [oldFormatter dateFromString:dateStr]; NSDateFormatter *newFormatter = [NSDateFormatter new]; [newFormatter setDateFormat:@"dd-MM-YYYY"]; I am using

How to calculate iOS app boot time

て烟熏妆下的殇ゞ 提交于 2019-12-31 02:35:07
问题 I was wondering if there is a quick and efficient way to determine the Time from opening an app to when it's fully loaded. I was thinking I'd do something like get an NSDate object in didFinishLaunchingWithOptions and get another in my UIViewController , then compare them. However, I'm not sure that would be completely accurate. Is there a better way to do this? 回答1: That really won't work because it doesn't deal with iOS loading and launching your app. The best you should do is log a

Problems retrieving NSDate object from Core Data using KVC

江枫思渺然 提交于 2019-12-30 12:13:50
问题 I have dates stored in core data as NSDate objects. When i try to retrieve them using a fetch request and -(id)valueForKey: i get an integer instead of an NSDate object. NSError *error = nil; NSArray *results = [managedObjectContext executeFetchRequest:request error:&error]; NSManagedObject *entity = [results lastObject]; NSDate *date = [entity valueForKey:@"updated"]; When i use dot notation such as myEntity.updated i get an NSDate object correctly but not when i use a KVC method. The reason

Why is Calendar.date(from: DateComponents) adding time?

守給你的承諾、 提交于 2019-12-30 11:32:11
问题 I create an instance of DateComponents using the following code: let dateComponents = DateComponents( calendar: .current, timeZone: Calendar.current.timeZone, era: nil, year: nil, month: nil, day: nil, hour: 9, minute: 0, second: 0, nanosecond: 0, weekday: 2, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil) I then print the dateComponents object and get the following (expected) output: calendar: gregorian (current) timeZone: Europe/London (current)

Getting human readable relative times and dates from a unix timestamp?

馋奶兔 提交于 2019-12-30 09:53:27
问题 Starting with a unix timestamp like 1290529723, how would I (assuming gmt) get the information on whether it is: today (if so, what time) in the last seven days (if so, which day ... mon, tues etc?) older than a week (if so, how to output in dd/mm/yy format?) I need this for a list of messages like the iPhone's Mail app, where date/times are shown relative to the current date and time, like so: 15:45 Yesterday Sunday Saturday 10/10/10 etc 回答1: It takes a bit of fiddling to get a solution that