nscalendar

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

iOS create date in the future ignoring daylight savings

荒凉一梦 提交于 2019-12-22 03:52:10
问题 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 *)firstDayOfNextMonthForDate:(NSDate*)date { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; calendar.timeZone = [NSTimeZone systemTimeZone]; calendar.locale = [NSLocale currentLocale]; NSDate *currentDate = [NSDate

iOS create date in the future ignoring daylight savings

江枫思渺然 提交于 2019-12-22 03:52:10
问题 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 *)firstDayOfNextMonthForDate:(NSDate*)date { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; calendar.timeZone = [NSTimeZone systemTimeZone]; calendar.locale = [NSLocale currentLocale]; NSDate *currentDate = [NSDate

Age extracted from birth date always off by inconsistent amount

强颜欢笑 提交于 2019-12-20 07:43:50
问题 I'm using the following code to convert a user-supplied birthdate to its equivalent years from the current date. The output is always off by an inconsistent amount in years and very large numbers in days and months. NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init]; [tempFormatter setDateFormat:@"yyyy-mm-dd hh:mm:ss"]; NSDate *birthDate = [tempFormatter dateFromString:[NSString stringWithFormat:@"%@-%@-%@ 01:00:00",self.birthYear.text,self.birthMonth.text,self.birthDay.text]];

NSDateFormatter reports June 2, 2013 as being in week zero

拥有回忆 提交于 2019-12-20 04:20:55
问题 I am using NSDateFormatter to convert a series of dates to a week number within a month. The date formatting code I am using is yyyyMMW and everything I have read tells me that W will be between 1-5. But, the 2nd of June 2013 fell on a Sunday (the default start day of the week in the gregorian calendar) and it's week number is reported as 0 even though the start date of the week is calculated correctly: 2013-06-03 14:15:45.611 date=20130531, week=2013055, start of week=20130526 2013-06-03 14

Calculate seconds between current time and next event

守給你的承諾、 提交于 2019-12-20 03:53:40
问题 This should be simple, but it's proving challenging for me. I'd like to know the best approach to calculating the difference in seconds between [NSDate date] and a future event x seconds from that time. There are several different types of event, and each event may occur several times a day, and at different times, depending what day of the week it happens to be. What I am trying to do is have the user select an event type from a picker and then set an alarm in Notification Center for the

iOS Get Date from Day of Year

六眼飞鱼酱① 提交于 2019-12-20 02:53:21
问题 The common question on stackoverflow is how to get the Day of Year from a date but how to you get the date from the Day of Year? I'm using the following code to generate the Day of Year but how to I do the converse? // Calculate Day of the Year NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]]; setDayOfYear = dayOfYear; return

iOS one month from current Date.

柔情痞子 提交于 2019-12-19 08:54:24
问题 I'm trying to get one month from the current date. Below is what I have and it is returning: 4027-09-24 16:59:00 +0000 . currentDate is right. What's going on? // Get todays date to set the monthly subscription expiration date NSDate *currentDate = [NSDate date]; NSLog(@"Current Date = %@", currentDate); NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit fromDate:currentDate]; dateComponents.month =

NSCalendar dateFromComponents: GMT timezone instead of systemTimeZone

纵然是瞬间 提交于 2019-12-19 07:38:09
问题 This code NSCalendar *calendar =[NSCalendar currentCalendar]; [gregorian setTimeZone:tz]; NSLog(@"%@", [gregorian timeZone]); NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]]; [comp setDay:info.day]; [comp setMonth:info.month]; [comp setYear:info.year]; [comp setHour:info.hour]; [comp setMinute:info.minute]; [comp setSecond:info.second]; [comp setTimeZone:tz]; NSLog(@"%@", [comp timeZone]); NSLog(@"%@", [gregorian