nsdate

NsDate formatter giving wrong Date from String [duplicate]

情到浓时终转凉″ 提交于 2019-11-28 09:57:42
问题 This question already has an answer here: NSDate Format outputting wrong date 5 answers I have the following code which takes a NSString and returns NSDate. I have copied this code from a project in which it runs perfectly fine - but some how this gives me the wrong output - (NSDate *)dateFromString:(NSString *)date { static NSDateFormatter *dateFormatter; if (!dateFormatter) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; } NSLog(@"Date: %@

How does one subtract hours from an NSDate?

流过昼夜 提交于 2019-11-28 09:38:10
I would like to subtract 4 hours from a date. I read the date string into an NSDate object use the following code: NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; NSDate * mydate = [dateFormatter dateFromString:[dict objectForKey:@"published"]]; What do I do next? NSDate *newDate = [theDate dateByAddingTimeInterval:-3600*4]; Link to documentation . NSDate *newDate = [[[NSDate alloc] initWithTimeInterval:-3600*4 sinceDate:theDate]] autorelease]; Link to documentation . NSCalendar is the general API for

ios- Check whether current time is between two times or not

半腔热情 提交于 2019-11-28 09:29:42
I want to check whether the current time is between two time or not. For example, I want to check if the current time is between today morning 10AM to 12PM or Tomorrow morning 10AM to 12PM. How can I do this? CRDave Try this NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; NSInteger currentHour = [components hour]; NSInteger currentMinute = [components minute]; NSInteger currentSecond = [components second]; if (currentHour < 7 || (currentHour > 21 || currentHour == 21 &&

Increase NSDate +1 day method Objective-C?

蓝咒 提交于 2019-11-28 09:12:41
This is my method I use for increasing by one day in my navigationBar, and setting the name of the day as a title. I know it's wrong because I set "today" variable every time its called. But I can't figure out how to increase +1 day every time I call this method. -(void)stepToNextDay:(id)sender { today = [NSDate date]; NSDate *datePlusOneDay = [today dateByAddibgTimeInterval:(60 * 60 * 24)]; NSDateFormatter *dateformatterBehaviour = [[[NSDateFormatter alloc]init]autorelease]; [dateFormatter setDateFormat:@"EEEE"]; NSString *dateString = [dateFormatter stringFromDate:datePlusOneDay]; self

Converting a date format in objective C

给你一囗甜甜゛ 提交于 2019-11-28 08:55:37
How can i convert the following date "Sun Jul 17 07:48:34 +0000 2011" to the following format "2011-07-17 07:48:34"? I used NSDateFormatter as shown below but it didnt work. It gives null as a result. NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init]; [objDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [objDateFormatter dateFromString:sDate]; Suhail Patel You've got your Date Format wrong for the style of Date you are passing in. Here is a document explaining the different modifiers: Date Format Patterns To parse the Date "Sun Jul 17 07:48:34 +0000 2011", you'd need a

Swift - Convert Milliseconds into Minutes, Seconds and Milliseconds

南笙酒味 提交于 2019-11-28 08:49:55
I am writing a racing app and want to convert a large number of milliseconds into minutes:seconds.milliseconds (for example, 1:32.361 ). At the moment I just do it through maths ( minutes / 60000 ), then get remainder and divide further, etc., but i find that it is off every now and then by 1 millisecond. I know this might not sound like a big deal, but I tried to find the average of 1:32.004 and 1:32.004 and it returned 1:32.003 , which means it gets put in incorrectly and the rest of the app can't deal with it. Is there a way to use NSDate or NSDateFormatter or something like that to format

Determine if today's date is in a range of two dates on iOS [duplicate]

你说的曾经没有我的故事 提交于 2019-11-28 08:42:15
Possible Duplicate: How to compare two dates in Objective-C I'd like to pop up a message in one of my apps when the date is in the range of two dates (e.g. the holiday period) So like if(dateInRangeof:date1, date2){True}else{false} Looking for any code snippets or apis to look at. Cheers - (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate { return [date compare:firstDate] == NSOrderedDescending && [date compare:lastDate] == NSOrderedAscending; } BDubCook How to compare two dates in Objective-C This should be what you're looking for. If you look in the

Compare two NSDates for same date/time [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 08:25:29
This question already has an answer here: How to compare two dates in Objective-C 14 answers Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:self]; NSDate* dateOnly = [calendar dateFromComponents:components]; return dateOnly; } - (BOOL) sameDayAsDate:(NSDate*)dateToCompare { NSDate *date1 = [self dateWithNoTime]

Nil NSDate when trying to get date from UTC string in zulu time

▼魔方 西西 提交于 2019-11-28 08:11:37
Writing an iPhone app in Objective-C, I have a date in string form (in UTC format, with a Z on the end to denote zero UTC offset, or zulu time), which I need to parse into an NSDate object. A bit of code: NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; NSString* str = @"2009-08-11T06:00:00.000Z"; NSDate* date = [df dateFromString:str]; Running this through the debugger, date ends up nil ! I'm assuming it has something to do with my date format string. How can I fix it to correctly parse the date string? A thought would be to make the Z in

NSDate from NSString

╄→гoц情女王★ 提交于 2019-11-28 07:46:29
问题 I've read the DateFormatting guide and I'm still not able to get a working formatter. NSString *string = @"0901Z 12/17/09"; //This is a sample date. The Z stands for GMT timezone //The 0901 is 09h 01m on a 24 hour clock not 12. //As long as I can get the hours/min & date from the string I can deal with the time zone later NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"hhmm'Z' MM/dd/yy"]; NSDate *date = [dateFormat dateFromString:string]; 回答1: That