Tag: nsdate

格林尼治标准时间到本地时间转换夏时制改变

从服务器我收到格林尼治标准时间结构(用户定义的结构),使用,我想将其转换为本地时间,我已经完成填充NSDatecomponent收到的结构,然后我已经使用dateformatter获取date它,除了一个案件,一切正常。 如果GMT时间在11月3日(美国夏令时改变)之后格式化器产生1小时时差。 例如:如果预计时间是11月3日下午4点,格林尼治标准时间从格林威治标准时间转换到当地时间为11月3日下午3点。 任何想法如何避免它。 编辑: // Selected Dates NSDateComponents *sel_date = [[NSDateComponents alloc]init]; sel_date.second = sch_detail.sel_dates.seconds; sel_date.minute = sch_detail.sel_dates.mins; sel_date.hour = sch_detail.sel_dates.hours; sel_date.day = sch_detail.sel_dates.date; sel_date.month = sch_detail.sel_dates.month; sel_date.year = sch_detail.sel_dates.year; sel_date.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; // Get the Date format. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; // Start_date formatter. NSDateFormatter *dateFormatter = […]

如何在每天的特定时间发送本地通知,即使时间已过?

我有这个代码每天早上7点运行一个通知,它获取当前的date,然后运行通知,当它达到设定的小时,我的问题是如果时间已经超过了设定的运行时间,那么每天它将运行在用户当前时间不是我上午7点的时间,这里是我的代码 var dateFire: NSDateComponents = NSDateComponents() var getCurrentYear = dateFire.year var getCurrentMonth = dateFire.month var getCurrentDay = dateFire.day dateFire.year = getCurrentYear dateFire.month = getCurrentMonth dateFire.day = getCurrentDay dateFire.hour = 7 dateFire.minute = 0 dateFire.timeZone = NSTimeZone.defaultTimeZone() var calender: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! var date: NSDate = calender.dateFromComponents(dateFire)! var localNotification = UILocalNotification() localNotification.fireDate = date localNotification.alertBody […]

在NSDate调用一个方法

我想在特定的时间和date(使用NSDate )调用一个方法,但我不能解决如何做到这一点。 我不想使用NSTimer因为当应用程序进入后台或被closures时,它将暂停。 我不想在后台运行一个,因为这只能解决一半的问题,而且在我看来这是一种浪费的方式。 我想,如果我将来可以在NSDate中触发一个方法,当应用程序再次打开时,我可以看到当前date是否已经通过了开始date,以及是立即调用还是继续等待。 我想我正在寻找像UILocalNotification ,没有通知。 我希望有一个简单的方法做到这一点,我似乎无法find,所以任何帮助将不胜感激。

将string转换为自定义格式的NSDate

可能重复: NSString到NSDate iPhone:如何将“yyyyMMddThhmmss”格式string转换为NSDate? 如何将此格式转换为NSDate? 我的stringdate格式是: yyyy-m-d'T'H:m:ss (间隔之间没有空格)。 更新: 2012-11-06T12:17:22.947 would be the string that I'd like to convert to NSDate

从NSDateFormatterFullStylestring中删除一年

我已经search了这个问题,没有find一个决定性的和优雅的解决scheme。 是否有改变NSDateFormatterFullStyleyear抑制年份信息。 我正在使用的代码: NSString *dateDescription; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setTimeZone:gmt]; [dateFormatter setDateStyle:NSDateFormatterFullStyle]; dateDescription = [dateFormatter stringFromDate:date]; NSLog(@"%@", dateDescription); 结果:2013年3月13日星期三 问候,马科斯

为什么不能使用<或>比较NSDate?

NSDate *date = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"MM/dd/yyyy"]; NSLog([@"today is " stringByAppendingString:[dateFormat stringFromDate:date]]); NSLog([@"firstBirthdayDate is " stringByAppendingString:[dateFormat stringFromDate:firstBirthdayDate]]); NSLog([@"secondBirthdayDate is " stringByAppendingString:[dateFormat stringFromDate:secondBirthdayDate]]); if ([firstBirthdayDate isEqualToDate:secondBirthdayDate]) NSLog(@"First date is the same as second date"); if (firstBirthdayDate < date) NSLog(@"First date is earlier than today"); else NSLog(@"First date is later than today"); […]

如何检查限制的date和时间是否与另一个限制重叠?

我想检查date限制* (例如: – 2013年7月22日2:30 PM -22/07/2013 8:30 PM) *是否在另一个时间限制内* (例如: – 2013年7月22日4: 30PM -22/07/2013 6:30 PM) *或不。 我对这个概念完全陌生。 任何人都可以请告诉我如何做到这一点。 任何帮助将不胜感激 下面是我试过的代码: – (void)viewDidLoad { [self isDateInterval:@"8/1/2013 8:30am" and:@"8/1/2013 8:40am" fallsWithin:@"8/1/2013 8:30am" and:@"8/1/2013 8:55am"]; [super viewDidLoad]; } -(BOOL)isDateInterval:(NSString*)startDate1 and:(NSString*)endDate1 fallsWithin:(NSString*)startDate2 and:(NSString*)endDate2 { BOOL isWithinrange; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"dd/MM/yyyy hh:mmaa"]; NSTimeInterval ti1 = [[formatter […]

在上午12点dynamic创build上个星期日的date

我正在努力弄清楚如何在上午12:00之前dynamic地创build最近一个星期天的date对象 我想我可以得到今天的date,然后减去本周的当天+ 1,在这一点上,我可以减去一天的时间下降到12点。 到目前为止,我有本周的这一天: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]]; int weekday = [comps weekday]; 在这一点上,我可以得到今天的date,并减去一天中的weekday几*秒的差异。 但是,如何在几秒钟内得到今天的时间?

iOS从当前date起一个月。

我试图从当前的date得到一个月。 下面是我所拥有的,它正在返回: 4027-09-24 16:59:00 +0000 。 currentDate是正确的。 这是怎么回事? // 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 = dateComponents.month + 1; NSDate *currentDatePlus1Month = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0]; NSLog(@"Date = […]

在iOS中将date转换为时间戳

你如何将任何给定的date转换为毫秒? 例如,2014-01-23到时间戳转换。 NSDate *date = [dateFormatter dateFromString:@"2014-01-23"]; NSLog(@"date=%@",date); NSTimeInterval interval = [date timeIntervalSince1970]; NSLog(@"interval=%f",interval); NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval]; [dateFormatter setDateFormat:@"yyyy/mm/dd "]; NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]); 产出成果:1970/30/01