This question already has answers here:
get time between two times of the day
(4 answers)
Closed 9 years ago.
I am working on Quiz App. In my app I want to get difference between quiz start time and end time. How to get difference between start and end time.
Thanks in advance.
You can use NSDate - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate for this purpose.
Use this
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yy"];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yy"];
NSString *dateString11 = [dateFormat stringFromDate:today];
NSDate *today1 = [dateFormatter1 dateFromString:dateString11];
NSDate *ExpDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"ExpDate"];
NSTimeInterval interval = [ExpDate timeIntervalSinceDate:today1];
int diff=interval/86400;
i hope this code useful for you.
And Another code for you.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDate *ServerDate = [dateFormatter1 dateFromString:#"04/26/2013"];
[dateFormatter1 release];
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate];
int diff=interval/86400;
NSLog(#"%d",diff);
The NSDate class has a method timeIntervalSinceDate that does the trick.
NSTimeInterval interval = [firstDate timeIntervalSinceDate:secondDate];
Related
In my application, i want to raise the notification using selected day and time, not with date.So i have to show the day and time only in DatePickerView. Would you please help me in this problem. Thanks in advance.
You should use NSDateFormatter for this purpose.
For example:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// e.g. "Tue 16:53"
[dateFormatter setDateFormat:#"EEEE HH:mm"];
NSString *szDate = [dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"Date formatted: %#", szDate);
(remove release if ARC is enabled);
And btw, the questions of this kind were asked million times around stackoverflow
I know this question is similar to others asked, but I can't find where my code is going wrong. I am trying to convert the current date into a mm/dd/yyyy format. Here is what I am using:
NSDate *date = [[NSDate alloc]init];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"mm/dd/yyyy"];
NSLog(#"%#", [formatter stringFromDate:date]);
The problem is the month. Every time I run this code, the month is different. For example the first time my date was 32/11/2012, and I just ran it and it was 55/11/2012.
Is there a reason why this would keep changing? The days and years appear to be fine.
Thanks.
Check the case:
mm means minutes
MM means months
So your code should probably read
NSDate *date = [[NSDate alloc]init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
NSLog(#"%#", [formatter stringFromDate:date]);
I am facing a weird problem with NSDate, when I try fetch a date from device, sometimes it shows previous month for some versions
Here is my chunk of code for reference
NSDate *date = [NSDate date];
NSDateFormatter *dateF = [[NSDateFormatter alloc]init];
[dateF setDateFormat:#" dd.MM.yyyy "];
NSString *selectedDate = [dateF stringFromDate:date];
Any inputs are appreciated, Thank you
To avoid later localizing problems you might use NSCalendar and its method components:fromDate:
Something like this:
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:[NSDate date]];
NSString * stringDate = [NSString stringWithFormat:#"%d.%d.%d", components.day, components.month, components.year];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
This is an example by Apple so it looks like you have your upper-case / lower-case right
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
Are you sure your device's date is set right? Also have you tried setting the locale of the NSDateFormatter?
EDIT:
To address you question in comments: NSDate is a point in time irrespective of time zones, calendars and so so on. NSCalendar and NSDateFormatter allow you to convert this point in time into a representation that is correct in a given time zone, with a given calendar.
I am developing one application. In that i write the below code for setting the DateFormat for current date. But it is working upto 9th month only. From 10th month onwards it gives the nil value. So please tell me how to solve this one. My code is as below:
NSDate *datestr = clInfo.cldrinfodate;
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *date1 = [dateFormat1 stringFromDate:datestr];
you can try this one.
NSDate * mCurrentDate = [NSDate date];
NSLog(#"current date=%#",mCurrentDate);
NSDateFormatter *dt=[[NSDateFormatter alloc] init];
[dt setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *st=[dt stringFromDate:mCurrentDate];
I have two strings both are in following date format (2011-03-22).
i have to compare them and find number of days between them.
Can anyone tell me how to do this..
Please also tell me the correct method to convert them back to NSDate.
Might be a couple of errors as i just typed this, but it speaks for itself and you should get the idea.
NSString *dateStringA = #"2011-03-22";
NSString *dateStringB = #"2011-03-27";
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
df.dateFormat = #"yyyy-MM-dd";
NSDate *dateA = [df dateFromString:dateStringA];
NSDate *dateB = [df dateFromString:dateStringB];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:dateA toDate:dateB options:0];
int daysBetweenDates = comps.day; //This is your days between the 2 dates
NSDate *intervalDate = [[NSCalendar currentCalendar] dateFromComponents:comps]; //This date object represents the duration between them
For string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];
for date to string
NSString *date = [[NSDate date] description];