Questions tagged [nscalendar]

NSCalendar is an Apple MacOS and iOS class to deal with calendrical computation.

a “toll-free bridged” counterpart to CFCalendar, an Objective-C component that provides information about the calendar and support for computations such as determining the range of a given calendrical unit and adding units to a given absolute time.

Reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html

612 questions
25
votes
3 answers

Swift 2.0 calendar components error

Hi I get this error in swift 2.0 Cannot invoke 'components' with an argument list of type '(NSCalendarUnit, fromDate: NSDate?, toDate: NSDate?, options: nil)' var hoy = dataFormatter.stringFromDate(NSDate()) var despues =…
Boss
  • 293
  • 1
  • 3
  • 11
25
votes
4 answers

Creating a future date in swift with NSDate()

I'm getting an error: "Missing argument for parameter 'coder' in call" for the following code: var components = NSDateComponents() components.setValue(1,forComponent: NSCalendarUnit.CalendarUnitMonth); var expirationDate =…
krisacorn
  • 831
  • 2
  • 13
  • 23
21
votes
1 answer

When does dateByAddingComponents:toDate:options return nil?

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,…
NickCE
  • 409
  • 3
  • 7
20
votes
3 answers

How to subtract date components?

As today is Friday, which is 6 according to NSCalendar. I can get this by using the following Calendar.current.component(.weekday, from: Date()) How do I get weekday component of Saturday last week, which should be 7? If I do …
Coder221
  • 1,403
  • 2
  • 19
  • 35
19
votes
2 answers

Making Date With Given Numbers

I have the following Swift (Swift 3) function to make a date (Date) with date components (DateComponents). func makeDate(year: Int, month: Int, day: Int, hr: Int, min: Int, sec: Int) -> NSDate { let calendar = NSCalendar(calendarIdentifier:…
El Tomato
  • 6,479
  • 6
  • 46
  • 75
16
votes
2 answers

Differences in NSDateComponents syntax?

I have been working on a clock app in Swift and with Xcode 6.3.2 the following code builds and runs just fine. // Get current time let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(.CalendarUnitHour…
zkarj
  • 657
  • 9
  • 23
15
votes
3 answers

Compare two NSDates for same date/time

Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar =…
Greg
  • 34,042
  • 79
  • 253
  • 454
13
votes
2 answers

Get current time as string swift 3.0

How to get current time as a String (not as Date) date time format. yyyy-MM-dd HH:mm:ss
GayashanK
  • 1,195
  • 2
  • 13
  • 27
12
votes
2 answers

UILocalNotification Repeat Interval for Custom Alarm (sun, mon, tue, wed, thu, fri, sat)

I'm using UILocalNotification for Alarm Purpose. I have a custom option for repeat based on weekdays (sun, mon, tue, wed, thu, fri, sat). So many applications did this process. I tried My level best. But I can't get it to work. Please you guys help…
Ganesh
  • 819
  • 3
  • 13
  • 27
12
votes
2 answers

Difference between NSCalendarUnitWeekOfYear & NSCalendarUnitWeekOfMonth

I want to make a UILocalNotification repeat every week, before iOS7 I would use localNotification.repeatInterval = NSWeekCalendarUnit - except NSWeekCalendarUnit has been deprecated. The docs say: "Use NSCalendarUnitWeekOfMonth or…
daihovey
  • 3,485
  • 13
  • 66
  • 110
12
votes
3 answers

How to get NSDate for 00:00 on the day of [NSDate date]?

I need to get an NSDate object for 00:00(beginning of the day) from [NSDate date], let's say if currently it is 11:30am(returned by [NSDate date]), on 01/06/2012, now I need to have an NSDate for 00:00am on 01/06/2012. I haven't tried this, but what…
hzxu
  • 5,753
  • 11
  • 60
  • 95
12
votes
4 answers

How to find what day of the week for any given date using Cocoa

I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you.
becu
10
votes
2 answers

How to determine first week day in iOS?

I am using Tapku's calendar in my application and I want to determine if the week should start on a Sunday or a Monday depending on the user's settings. I am calling firstWeekday, but for some reason it returns 1 (Sunday) on a device where the…
Nikolay Spassov
  • 1,318
  • 1
  • 12
  • 22
10
votes
4 answers

iOS create date in the future ignoring daylight savings

I'm trying to work with dates and create dates in the future, but daylight savings keeps getting in the way and messing up my times. Here is my code to move to midnight of the first day of the next month for a date: + (NSDate…
Josh
  • 3,445
  • 5
  • 37
  • 55
9
votes
2 answers

Save nsdate to disk

I've looked at the timeIntervalSinceReferenceDate function of NSDate. Can I use this function to store the interval to disk and then return it to NSDate with the same value as the original? I'm wary that the reference or interval could vary between…
the Reverend
  • 12,305
  • 10
  • 66
  • 121
1
2
3
40 41