I created a custom calendar for my calendar app ... but I have some problem which I would be grateful if you help me to do this , this code shows a single day .
for example day 2 , 4 , 13 , 24
- (NSString *) showPersianDay {
NSCalendar *persCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSPersianCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *IRLocal = [[NSLocale alloc] initWithLocaleIdentifier:#"fa_IR"];
[dateFormatter setLocale:IRLocal];
[dateFormatter setCalendar:persCalendar];
offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.day = _dayNumber;
NSDate *nextDate = [persCalendar dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];
[dateFormatter setDateFormat:#"d"];
NSString *currDay = [dateFormatter stringFromDate:nextDate];
[NSString stringWithFormat:#"%#",currDay];
[persCalendar release];
[dateFormatter release];
return currDay;
}
I created a custom table for a month which shows days of a month , so when user tap on this number my date should change according to the numbers ,
for example today is 4 Jan -- when user tap 6 Jan my date should change to the 6 Jan.
I don't have to use some way like : day.text = #"6" or something .... I need return a real DATE, How can I modify my code to show specific day ?
thank you.
Maybe you should have the class return an NSDictionary with multiple keys. One key can contain the string value for the day number, the other can contain the date object.
- (NSDictionary *) showPersianDay {
NSCalendar *persCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSPersianCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *IRLocal = [[NSLocale alloc] initWithLocaleIdentifier:#"fa_IR"];
[dateFormatter setLocale:IRLocal];
[dateFormatter setCalendar:persCalendar];
offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.day = _dayNumber;
NSDate *nextDate = [persCalendar dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];
[dateFormatter setDateFormat:#"d"];
NSString *currDay = [dateFormatter stringFromDate:nextDate];
[NSString stringWithFormat:#"%#",currDay];
NSDictionary *results = [NSDictionary dictionaryWithObjectsAndKeys:currDay,#"dateString",nextDate,#"DateObj" nil];
return results;
}
Then on the other side you can display the dateString and cache the DateObj in the calendar cell. Hopefully this solves your question.
Related
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];
I have 2 date pickers, each one deference from the ether (280 day)
the pickers change correctly, but mydatepicker.date when NSlog it or put it inUITextfield the date become date - 1 day or date + 1 day
here is my code :
the first picker:
int daysToAdd = -280;
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];
// create a calendar
NSCalendar *addingdays = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate = [addingdays dateByAddingComponents:components toDate:dueDatePickerView.date options:0];
lastPerPickerView.date = newDate;
NSLog(#"%#",newDate);
the second one:
int daysToAdd2 = 280;
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd2];
// create a calendar
NSCalendar *addingdays2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate2 = [addingdays2 dateByAddingComponents:components toDate:lastPerPickerView.date options:0];
dueDatePickerView.date = newDate2;
NSLog(#"%#",newDate2);
if the date of 1st picker is 12-12-2012
the out put is 11-12-2012
in my code i gave them the properity of
lastPerPickerView.datePickerMode = UIDatePickerModeDate;
Try adding this lines to your code,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
or
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
The output you are getting is correct logically. But if you want the output according to your requirement I think you should change value 280 to 279.
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];
I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = #"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t comes back as 0 every time.
Use NSDateFormatter to get a NSDate then use - (NSTimeInterval)timeIntervalSince1970 to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2011-01-12T14:17:55.043Z"];
NSLog(#"Date: %#", date);
NSLog(#"1970: %f", [date timeIntervalSince1970]);
NSLog(#"sDate: %#", [formatter stringFromDate:date]);
[formatter release];