nscalendar

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

邮差的信 提交于 2019-11-29 20:16:29
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 RETURN 'Today' ELSE IF days_from_today(timestamp) == 1 RETURN 'Tomorrow' ELSE IF days_from_today(timestamp) < 1 RETURN days_from_today(timestamp) + ' days ago' ELSE RETURN 'In ' + days_from_today(timestamp) + ' ago' Crucially though, it needs to be in Swift and I'm struggling with the NSDate / NSCalendar objects. I started with working out the time

Changing Gregorian Month and Days names to Islamic? NSDate

ⅰ亾dé卋堺 提交于 2019-11-29 14:25:24
问题 I'm trying to display the Islamic current day name , month name. I used NSLocale to set the region to ar_SA , however it's just translating the day and month names from English to Arabic. Output I'm getting in Arabic: ١٢ الأحد ، اكتوبر ، ٢٠١٢ which in English is Sunday, October 28, 2012 Output I want: Al-Ahad, Dul-Hijja 12, 1433 Snippet: NSDateFormatter *islamicFormatter = [[NSDateFormatter alloc]init]; [islamicFormatter setDateStyle:NSDateFormatterFullStyle]; [islamicFormatter setLocale:[

NSDate for first day of the month

蹲街弑〆低调 提交于 2019-11-29 10:48:47
问题 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 ) another NSDate object will be returned (let's call this firstDayOfMonthDate ) which represents the first day of the month in arbitraryDate . The time component does not matter, as I just need the NSDate object for the first day of the month

NSCalendar, why does setting the firstWeekday doesn't effect calculation outcome?

风格不统一 提交于 2019-11-29 10:36:34
i need to calculate the weekday for a given date, however, depending on the calendar a week can star on Monday and somwhere on Sunday so i wanted to set it, to start on Monday, using [[NSCalendar currentCalendar] setFirstWeekday:2]; however, the calculation outcome is the same { [[NSCalendar currentCalendar] setFirstWeekday:1]; NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date]; NSInteger weekday = [weekdayComponents weekday] - 1; NSLog(@"%d", weekday); } { [[NSCalendar currentCalendar] setFirstWeekday:2];

Difference between NSCalendarUnitWeekOfYear & NSCalendarUnitWeekOfMonth

烈酒焚心 提交于 2019-11-29 09:26:27
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 NSCalendarUnitWeekOfYear, depending on which you mean" But I don't know what the difference is, so I don't know which one I mean. These don't appear to be documented but most likely NSCalendarUnitWeekOfYear will be a value from 1-53 (or maybe 0-52) representing the week number within the calendar year while NSCalendarUnitWeekOfMonth will be 1-5 (or maybe 0-4)

Get current time as string swift 3.0 [duplicate]

好久不见. 提交于 2019-11-29 03:52:13
This question already has an answer here: Converting NSString to NSDate (and back again) 17 answers How to get current time as a String (not as Date) date time format. yyyy-MM-dd HH:mm:ss This is the way I figure it out. // Get today date as String func getTodayString() -> String{ let date = Date() let calender = Calendar.current let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date) let year = components.year let month = components.month let day = components.day let hour = components.hour let minute = components.minute let second = components.second

Swift 3 - find number of calendar days between two dates

允我心安 提交于 2019-11-28 18:26:27
问题 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 start of the day currentCalendar.rangeOfUnit(.Day, startDate: &startDate, interval: nil, forDate: currentDate) currentCalendar.rangeOfUnit(.Day, startDate: &endDate, interval: nil, forDate: self) let intervalComps = currentCalendar.components([.Day], fromDate:

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

核能气质少年 提交于 2019-11-28 15:46:06
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. Oded Ben Dov @Chuck's answer is correct, and lead me to the following code. Thought I'd share: NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:10]; [comps setMonth:10]; [comps setYear:2010]; NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps]; You can use this NSString *dateString = @"03-Sep-11"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"dd-MMM-yy"; NSDate *date = [dateFormatter

Why NSCalendar's dateFromComponents returns wrong date?

别说谁变了你拦得住时间么 提交于 2019-11-28 13:46:13
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 *retDate = [cal dateFromComponents:comps]; KingofBliss I think your problem may be solved by setting

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

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:57:56
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. I've been doing: NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [theDateFormatter setDateFormat:@"EEEE"]; NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]]; This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call Lots more information at: http://developer.apple.com