Menu
  • HOME
  • TAGS

Detect If between time periods

ios,nscalendar,nsdatecomponents

I fixed this issue by replacing the 'if' line with: if ( [components hour] <= 5 && [components hour] >= 9){ and removing the NSUInteger line....

Calculation of DayInEra inconsistent with DayInYear

xcode,cocoa,nscalendar

When using the era bit, the era is based on an absolute moment in time (when JC was born). The fact that your local clock passed midnight from dateLastNite to dateMorning does not guarantee that the moment in time passed a 24 hour crossover (as computer from the beginning of...

24 hours divided by 30 minutes apart - NSDate

objective-c,nsmutablearray,nsdate,nscalendar,nsdatecomponents

You're not zeroing out the seconds in your changeTimeValue, and in any case I think you've overcomplicated it. So I suspect you're subsequently seeing rounding issues. Just use NSDateComponents to do the whole job: NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDateComponents *dateComponents = [currentCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit...

Replacement for deprecated .WeekdayCalendarUnit

ios,xcode,swift,nscalendar

A little tricky. Check the format. Here you go: let weekdayComponent = currentCalendar?.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: date) Or: let weekdayComponent = currentCalendar?.components(.CalendarUnitWeekday, fromDate: date) I was able to find the right format in the NSCalendar Class Reference...

Horizontal Calendar in iOS

ios,iphone,uiscrollview,nscalendar

You can use library that is most closest to functionality you required and can customize its appearance as per you need. https://github.com/CapitalGene/objc-CGCalendarView ...

Local Notification Repeat Every Weekend Day

ios,ios8,uilocalnotification,nscalendar

There is no "weekend" unit, and there is no way to specify a custom repeat. You will need to create two notifications and schedule one for every Saturday, the other for every Sunday.

Neat way to get end of day for date?

ios,nscalendar,nstimeinterval

A better way would be to get the start of the next day and subtract 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 second. Do you get what I want to say? It's very hard to define the end of the day. 23:59 is certainly not the end of the day, there is almost a whole minute...

Creating NSDate from components without “timezone correction”

ios,swift,nsdate,nscalendar,nsdatecomponents

There is no need to use NSDateComponents to input time. NSCalendar already has a method for you to input your date using your local time. It is called dateWithEra. "+0000" it is not your localTime (CET) it is UTC time. Your timeZone is not stored with your NSDate object, only...

NSDateComponents doesn't return date

ios,swift,nsdate,nscalendar,nsdatecomponents

If you want to use NSDateComponents in this direction you have to set the calendar. let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)! let components = NSDateComponents() components.calendar = calendar // Add this line components.hour = hours.toInt()! components.minute = minutes.toInt()! let neededDate = components.date ...

Get Week Start date returning next week day if today is sunday

ios,nsdate,nscalendar

In your calculation you would have to check if [components weekday] is less or greater than 2 (Monday) and modify the components accordingly to get the previous Monday. But you can simplify the calculation to: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // [gregorian setLocale:[NSLocale currentLocale]]; (This is the default.) [gregorian...

NSCalendar.dateFromComponents returns wrong time

ios,swift,nsdate,nscalendar

You can solve your problem by setting the timezone correctly. For example : calendar?.timeZone = NSTimeZone(abbreviation: "GMT")! ...

Find all days of a week/month/year and add to NSMutableArray

ios,objective-c,nsdate,nscalendar

You're on your way. The key is to advance the component's day in a loop and extract a date from them. NSMutableArray *result = [NSMutableArray array]; NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *startDate = // your input start date NSDate *endDate = // your input end date NSDateComponents *comps =...

How to get start date of current year in iOS

ios,objective-c,nsdate,nscalendar

-(NSDate *)getFirstDateOfCurrentYear { //Get current year NSDate *currentYear=[[NSDate alloc]init]; currentYear=[NSDate date]; NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init]; [formatter1 setDateFormat:@"yyyy"]; NSString *currentYearString = [formatter1 stringFromDate:currentYear]; //Get first date of current year NSString *firstDateString=[NSString stringWithFormat:@"10 01-01-%@",currentYearString]; NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init]; [formatter2 setDateFormat:@"hh dd-MM-yyyy"]; NSDate *firstDate =...

NSCalendar week number of date

ios,swift,nscalendar

Seems like your posted line has no error. Just check the below solution to confirm if result is same. Note: Just trying to eliminate any possiblity of irrelevant issue due to date. For Swift you can use. let calender = NSCalendar.currentCalendar() let dateComponent = calender.components(NSCalendarUnit.WeekOfYearCalendarUnit | NSCalendarUnit.DayCalendarUnit | NSCalendarUnit.MonthCalendarUnit |...

Create own NSDate

swift,xcode6,nsdate,nscalendar

That can be done by using NSDateComponents var dateComponents = NSDateComponents() dateComponents.second = 0 dateComponents.minute = 0 dateComponents.hour = 0 dateComponents.day = 1 dateComponents.month = 1 dateComponents.year = 1 dateComponents.nanosecond = 0 let date = NSCalendar.currentCalendar().dateFromComponents(dateComponents) ...

Can't init static calendar in initialize method of NSDate category

ios,objective-c,nsdate,nscalendar

When you use a category to override an existing method, you overwrite the original implementation, so NSDate's original +initialize method never gets executed. In this case, it looks like it's doing something in that method that's necessary for its own internal functionality, and this is breaking your app. Even if...

Pretty Date Intervals in Swift. NSCalendar components

ios,swift,nscalendar

There are two errors: The first parameter in a method does not have an external parameter name, so you must not specify unitFlags:. For "no options", specify NSCalendarOptions(0) or simply nil. Together: let components = calendar.components(.CalendarUnitDay, fromDate: date, toDate: now, options: nil) Update for Swift 2: NS_OPTIONS are now imported...

How to get actual first day of the month, possible with my time zone?

ios,objective-c,cocoa-touch,nscalendar

I've tried this myself: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[NSDate date]]; [dateComponents setDay:1]; NSLog(@"%@", [gregorian dateFromComponents:dateComponents]); And it properly prints 2015-04-30 22:00:00 +0000 which would be 1st of May after adding 2 hour (in my case) timezone difference....

(OBJ C) Sending message calendarIdentifier to an instance of NSCalendar and receiving null

objective-c,null,nsdate,nscalendar

You need to supply a known calendar type to the initWithCalendarIdentifier: initializer. Just providing any string doesn't work. The known types are: NSGregorianCalendar NSBuddhistCalendar NSChineseCalendar NSHebrewCalendar NSIslamicCalendar NSIslamicCivilCalendar NSJapaneseCalendar NSRepublicOfChinaCalendar NSPersianCalendar NSIndianCalendar NSISO8601Calendar This will produce a non-null value for the identifier: message. I found these constants in NSLocale.h....

'String' is not convertible to 'Int'

swift,int,nsdate,nscalendar

You're getting that error because you're trying to add (aka, use +) String and Int instances together. You could instead use string interpolation: let time = "\(hour):\(minutes):\(second)" or better yet, learn about NSDateFormatter, which handles this much better: let timeFormatter = NSDateFormatter() timeFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("HH:mm:ss", options: 0, locale: NSLocale.currentLocale()) let...

Something wrong with nscalendar [duplicate]

ios,nsdate,nscalendar

From Apple docs: Returns the number of hour units for the receiver. - (NSInteger)hour Return Value: The number of hour units for the receiver. Discussion: This value is interpreted in the context of the calendar with which it is used Calendar context makes hour component depending on time zone that...

Why my comparison of two dates doesn't work as it should?

ios,swift,nscalendar

When you output a date, don't use simple println, which gives you the date/time in London. Instead, do it like this: println(date.descriptionWithLocale(NSLocale.currentLocale())) That will give you a clearer idea of what date-time is involved, because it will correspond to where you are located. If you do that with your dates,...

How to get weekends vs weekdays in Cocoa?

cocoa,locale,nsdateformatter,nscalendar

I finally got it. The mistake was in the locale identifier which should be en_AE instead of en_AR import UIKit let arabicLocale = NSLocale(localeIdentifier: "en_AE") let islamicTabularCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicTabular) islamicTabularCalendar?.locale = arabicLocale let islamicTabularFormatter = NSDateFormatter() islamicTabularFormatter.calendar = islamicTabularCalendar islamicTabularFormatter.dateStyle = NSDateFormatterStyle.FullStyle let gregorianCalendar = NSCalendar(calendarIdentifier:...

UILocalNotification with repeatInterval set NSWeekdayCalendarUnit doesn't trigger weekly

ios,objective-c,uilocalnotification,nscalendar

PLz Try this UILocalNotification *localNotif=[[UILocalNotification alloc]init]; localNotif.fireDate =currentDate; localNotif.timeZone=[NSTimeZone defaultTimeZone]; localNotif.alertBody = @"MazeRoll"; localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 1; localNotif.repeatInterval=NSWeekCalendarUnit; UIApplication *app=[UIApplication sharedApplication]; [app scheduleLocalNotification:localNotif]; NSLog(@"sdfsdfsdf%@",[[UIApplication sharedApplication]...

NSCalendar set firstWeekDay to monday but results are different

ios,iphone,nsdate,nscalendar

It seems you are getting the right result. Depending on the time zone of your Xcode installation, the console will output a different date. Try using a date formatter in NSLog. NSDateFormatter *f = [[NSDateFormatter alloc] init]; NSDate *firstMonday = [self beginningOfMonth:[NSDate date]; NSLog(@"First Monday of month: %@", [f stringFromDate:firstMonday]);...

Using a variable property value for NSCalendarIdentifier return nil

ios,nsdate,nscalendar

Your problem comes from the way you store the calendar identifier in your variable. When you wrote @"NSCalendarIdentifierIslamic" it simply means a string with that characters, and not the string that the NSCalendarIdentifierIslamic identifier points to. So simply set your variable jam.calenderType = NSCalendarIdentifierIslamic; or jam.calenderType = NSCalendarIdentifierGregorian; and it...

Why compareDate from NSCalendar seems to need to set an UTC timeZone to work properly?

ios,timezone,nsdate,nscalendar

The timezone comes into play because you compare the dates with a granularity that is timezone dependent. So you are actually comparing against the local representation of the date. The point in time model that is often used to describe NSDate doesn't know about days and weeks. From a abstract...

Detect when it is evening/night - NSDate - iOS

ios,objective-c,time,nsdate,nscalendar

Just in case you are allowed to use Weather API. You can use the following library: OpenWeatherMapAPI It gives the data in the following format: NSDictionary that looks like this (json): { coord: { lon: 10.38831, lat: 55.395939 }, sys: { country: "DK", sunrise: 1371695759, // this is an NSDate...

Get Time Format (AM/PM or 24hrs) from Current Calendar / Locale

osx,nsdateformatter,nscalendar,nslocale

According to http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns: 'j' is a special purpose symbol for flexible date pattern generation. It requests the preferred hour format for the locale and must be replaced by 'h', 'H', 'K' or 'k'. 'h' and 'K' are symbols for 12-hour-cycle formats. 'H' and 'k' are symbols for 24-hour-cycle formats. So...

calendar.component and unrecognized selector sent to instance error in my iOS app (Swift)

ios,swift,uiwebview,ios7.1,nscalendar

I will help you fix your code. First suggestion is to use NSCalendarUnit to subtract one day or week or month from any NSDate type of calculation. let yesterday = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: -1, toDate: NSDate(), options: nil)! To find out the day of the week you will need to use...

Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'?

objective-c,delegates,nsdate,nscalendar,ios8.1

- (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height; expects an NSCalendar as the first arg and you are passing a NSDate. Either you are passing a date when you should be passing a calendar or the delegate method should be updated to take a date arg and not an NSCalendar argument....

Find the time/date from min./hours/seconds swift 1.2 xcode 6.3

swift,nsdate,nscalendar

To calculate the endtime you can use timerEndTime = NSDate(timeIntervalSinceNow:NSTimeInterval(oreWanted * 11)) So you get a NSDate object. Now you can fire a UILocalNotification at that calculated time: Note: You need to ask for notification permissions when your app starts up. I have simplified this here with notificationsAllowed and soundsAllowed...