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
295
votes
17 answers

NSDate get year/month/day

How can I get the year/month/day of a NSDate object, given no other information? I realize that I could probably do this with something similar to this: NSCalendar *cal = [[NSCalendar alloc] init]; NSDateComponents *components = [cal components:0…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
158
votes
20 answers

How to determine if an NSDate is today?

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…
Seunghoon
  • 5,632
  • 5
  • 35
  • 41
115
votes
12 answers

first and last day of the current month in swift

I'm trying to get the first and last day of the month in swift. So far I have the following: let dateFormatter = NSDateFormatter() let date = NSDate() dateFormatter.dateFormat = "yyyy-MM-dd" let calendar = NSCalendar.currentCalendar() let components…
user2363025
  • 6,365
  • 19
  • 48
  • 89
114
votes
21 answers

Get day of week using NSDate

I created a method that is supposed to take in a string in "YYYY-MM-DD" form and spit out an int that represents the dates position in relation to the week it is in (regardless if it overlaps between months). So e.g sunday=1 monday=2 and so on.…
boidkan
  • 4,691
  • 5
  • 29
  • 43
110
votes
6 answers

Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago

I'm trying to work out how to decide if a given timestamp occurs today, or +1 / -1 days. Essentially, I'd like to do something like this (Pseudocode) IF days_from_today(timestamp) == -1 RETURN 'Yesterday' ELSE IF days_from_today(timestamp) == 0…
Ben
  • 4,707
  • 5
  • 34
  • 55
100
votes
8 answers

iPhone OS: How do I create an NSDate for a specific date?

Seems like a simple thing but I can't seem to find a way to do it. It would be great to see a couple different methods.
nickthedude
  • 4,925
  • 10
  • 36
  • 51
71
votes
4 answers

Number of days in the current month using iOS?

How can I get the current number of days in the current month using NSDate or something similar in Cocoa touch?
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
62
votes
11 answers

Swift 3 - find number of calendar days between two dates

The way I did this in Swift 2.3 was: let currentDate = NSDate() let currentCalendar = NSCalendar.currentCalendar() var startDate : NSDate? var endDate : NSDate? // The following two lines set the `startDate` and `endDate` to the…
Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
59
votes
9 answers

How can I set an NSDate object to midnight?

I have an NSDate object and I want to set it to an arbitrary time (say, midnight) so that I can use the timeIntervalSince1970 function to retrieve data consistently without worrying about the time when the object is created. I've tried using an…
AstroCB
  • 12,337
  • 20
  • 57
  • 73
57
votes
3 answers

How can I get the current month as String?

I need to get may as a current month, but I could not do. How can I achieve this? let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day , .Month , .Year], fromDate: date) let year…
iron
  • 715
  • 1
  • 6
  • 15
45
votes
7 answers

All dates between two Date objects (Swift)

I’m creating a date using NSDateComponents(). let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate =…
ixany
  • 5,433
  • 9
  • 41
  • 65
44
votes
17 answers

NSCalendar first day of week

Does anyone know if there is a way to set the first day of the week on a NSCalendar, or is there a calendar that already has Monday as the first day of the week, instead of Sunday. I'm currently working on an app that is based around a week's worth…
Joshua
  • 839
  • 4
  • 12
  • 15
29
votes
5 answers

NSDate for first day of the month

This is quite a simple concept, but as of yet I have been unable to find an elegant (and calendar locale independent) solution. I need to find the first day of the month for an arbitrary NSDate. For example, given an arbitrary NSDate (arbitraryDate)…
Skoota
  • 5,280
  • 9
  • 52
  • 75
26
votes
6 answers

how do I set an existing NSDate's time?

how do I set an existing NSDate's time? That is I have a date (say current date/time), but then want to set this to a specific time (e.g. 11.23am) say. What is the quickest way to do this in objective C? I'm looking at NSCalendar and see methods…
Greg
  • 34,042
  • 79
  • 253
  • 454
26
votes
8 answers

How to get Monday's date of the current week in swift

I'm trying to get Monday's date of the current week. This is treated as the first day of the week in my table view. I also need to get Sunday's of the current week. This is treated as the last day of the week in my table view. Current attempt: let…
user2363025
  • 6,365
  • 19
  • 48
  • 89
1
2 3
40 41