nsdatecomponents

NSDateComponents components:fromDate and Time Zones

做~自己de王妃 提交于 2019-12-30 00:36:19
问题 I have a method which extracts the hour and second components form a NSDate by breaking it down into its NSDateComponents. My code is as follows... unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit; NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSDateComponents* travelDateTimeComponents = [calendar components:hourAndMinuteFlags fromDate:travelDate]; NSString* hours

Why NSCalendar's dateFromComponents returns wrong date?

柔情痞子 提交于 2019-12-29 01:38:34
问题 I am expecting 2010-10-11 00:00:00 But instead I receive 2010-10-10 21:00:00 +0000 I know, that I can add a few hours and receive desired date, but I do not understand why I get previous day with 21 hours... Please, help! NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:2010]; [comps setMonth:10]; [comps setDay:11]; [comps setHour:0]; [comps setMinute:0]; [comps setSecond:0]; NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate

Get the last weekday of a month

混江龙づ霸主 提交于 2019-12-25 00:29:06
问题 I want to get the last weekday of a given month. I've come across this simple answer on how to get the 1st/2nd/... weekday which works well. The question is: How to get the last weekday of a given month? Not every month has only 4 Sundays, so do I have to count the number of Sundays of the month, or is there a more elegant way to do this? 回答1: Had the same need recently, and the best I could come up with is the following, which is meant to be run every day to check if the current day is the

How to create dateComponents from Date stored in CoreData

╄→尐↘猪︶ㄣ 提交于 2019-12-24 21:52:18
问题 I am trying to create dateComponents from Date stored in CoreData from UIDatePicker. However, I am not able to make it. I am first converting Date which is stored in editnotes?.sSelectedDate into dateComponents and then using the dateComponents for triggering my notifications. Here is what I am doing: var date = Date() if let editnote = editnotes { date = editnote.sSelectedDate } else { print("nil") } let calendar = NSCalendar.current let unitFlags = Set<Calendar.Component>([.day, .month,

Finding time between dates in Swift [duplicate]

痴心易碎 提交于 2019-12-24 13:41:24
问题 This question already has answers here : Getting the difference between two NSDates in (months/days/hours/minutes/seconds) (18 answers) Closed 4 years ago . Hi Im trying to work out the time between two dates using UIpickers and I am lost. I cannot fathom where to go to on this to get the value to plug into the label. Im not sure if I should use components or before the comparison or just after to put it in the label. Thanks for any pointers. let calendar = NSCalendar.currentCalendar() let

Why does my DateComponents object in iOS not save to or retrieve from CloudKit correctly?

回眸只為那壹抹淺笑 提交于 2019-12-24 07:36:12
问题 I am using CloudKit with Swift for iOS. I am saving an object of type DateComponents in a CloudKit field of type Bytes. When I retrieve the object, it does not have the same value as the object I originally saved. Here is the code where I save the object to CloudKit: let unsafePointer: UnsafePointer<DateComponents> = UnsafePointer<DateComponents>(&time) let unsafeBufferPointer: UnsafeBufferPointer<DateComponents> = UnsafeBufferPointer<DateComponents>(start: unsafePointer, count: 1) let data:

NSDateComponents: difference between weekOfYear and weekOfMonth

泄露秘密 提交于 2019-12-23 08:48:30
问题 In my app, I have to set up a weekly reminder so that an alert goes off at the same day/time, a week from now. I have been using NSDateComponenents.week = 1 and adding that to an NSDate, using NSCalendar's dateByAddingComponents:toDate:options: API. It seems that week is now being deprecated in iOS7, and the warning tells me to "Use weekOfMonth or weekOfYear, depending on which you mean". But I can't find much documentation about what either of them means. Can anyone explain the difference

Creating NSDate from components without “timezone correction”

假装没事ソ 提交于 2019-12-23 07:00:30
问题 I have a function func createDate(day: Int, month: Int, year: Int) -> NSDate { var comp = NSDateComponents() comp.day = day comp.month = month comp.year = year var cal = NSCalendar.currentCalendar() if var date = cal.dateFromComponents(comp) { return date } return NSDate() } If I call it with createDate(07, 01, 1984) the output is 1984-01-06 23:00:00 +0000 . I assume that there is some influence from the time zone. How can I adjust my code so that the output will be 1984-01-07 00:00:00 +0000

Swift - Date from components is incorrect when printed. Date has 2 values?

此生再无相见时 提交于 2019-12-22 18:35:13
问题 I'm trying to store a time of day in a Date: let calendar = NSCalendar.init(identifier: .gregorian) let components = NSDateComponents() components.hour = 7 components.minute = 0 var newDate = calendar?.date(from: components as DateComponents)! print(newDate!) However, this yields some bizarre results when I try to print or otherwise use the value. Here's a Swift Playground of the results: How can newDate be both 7:00AM and 11:56AM at the same time? 回答1: You didn't specify a time zone (by

Getting first and last days of current week

不羁岁月 提交于 2019-12-22 12:28:00
问题 How can I get first and last days of the current week? What I need is some way of filtering a set of objects with NSDate property to leave only ones, that appear on current week, and then filter them by days. Also I need some way to get localized day names. I've tried to play with NSCalendar, NSDate and NSDateComponents, but it seems that I can't just check calendar's firstWeekday, as there might be weeks with days from previous/next month. 回答1: Use NSDateComponents to get the weekday, which