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.
Related
Currently have a text input from users and which to add that to current time and display on the Screen as a text input in HH:mm format.
Current Code:-
NSString *strCurrentDate;
NSString *strNewDate;
NSDate *date = [NSDate date];
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"hh:mm"];
strCurrentDate = [df stringFromDate:date];
NSLog(#"Current Time: %#",strCurrentDate);
int minutesToAdd = workingTime.text;
NSCalendar *calendar = [[NSCalendar
alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMinute:minutesToAdd];
NSDate *newDate= [calendar dateByAddingComponents:components toDate:date options:0];
[df setDateFormat:#"hh:mm"];
strNewDate = [df stringFromDate:newDate];
NSLog(#"workingtime is :%#",workingTime.text);
NSLog(#"New Date and Time: %#",strNewDate);
crewOutTime.text = strNewDate;
However Add's incorrect amount to time When i Change it to int minutesToAdd = 40; to a fixed Value it is correct and works as wanted.
Try
int minutesToAdd = workingTime.text.intValue;
instead of
int minutesToAdd = workingTime.text;
With your code you are setting your minutesToAdd to a pointer.
I am new to iPhone developer,
I made a one custom date in that custom date i want to add 1 month or 3 month or 6 month according to frequency.
i want to add month till newDate is less then today's date.
here is my code snippet,
while ([today compare:temp] == NSOrderedDescending)
{
NSLog(#"inside----- today=%#,temp=%#",today,temp);
//add to dates
NSDateComponents *newComponents= [[NSDateComponents alloc] init];
if([FrequencySelText isEqualToString:#"Monthly"]){
[newComponents setMonth:1];
}
if([FrequencySelText isEqualToString:#"Quarterly"]){
[newComponents setMonth:3];
}
if([FrequencySelText isEqualToString:#"Half - Yearly"]){
[newComponents setMonth:6];
}
if([FrequencySelText isEqualToString:#"Yearly"]){
[newComponents setMonth:12];
}
// get a new date by adding components
NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSString *newDate = [formatter stringFromDate:temp];
NSLog(#"new date=%#",newDate);
}
My Log shows: inside----- today=2012-07-11 14:41:27 +0000,temp=2012-04-12 03:00:00 +0000
I am getting Exc_bad_access at this line, NSDate* temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0];
Any help will bw appreciated.
Providing value to temp again then no need provide NSDate object type again. Here u already have NSDate *temp then why r u using same name again for NSDate object
temp= [[NSCalendar currentCalendar] dateByAddingComponents:newComponents toDate:temp options:0]
Maybe try this one:
NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[dateComponents setMonth:1]; // Sample Add
NSDate *tempDate = [gregorian dateByAddingComponents:dateComponents toDate: temp options:0];
NSString *strTempDate = [dateFormat stringFromDate:tempDate];
// New Date on strTempDate
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.
How will I compare current time [NSDate date] with fixed time 05:00:00 PM.
That 05:00 PM is already passed or not. I just need BOOL check for this.
- (BOOL)past5pm
{
NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [gregorianCalender components:NSHourCalendarUnit fromDate:[NSDate date]];
if([components hour] >= 17) // NSDateComponents uses the 24 hours format in which 17 is 5pm
return YES;
return NO;
}
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH.mm"];
NSDate *currentDate = [NSDate date];
NSString *curDate = [dateFormatter stringFromDate:currentDate];
if ([curDate doubleValue] >= 17.00)
{
//set your bool
}
Try this
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm:ss a"];
NSDate* date = [NSDate date];
//Get the string date
NSString* str = [dateFormatter stringFromDate:date];
NSDate *firstDate = [dateFormatter dateFromString:#"05:00:00 PM"];
NSDate *secondDate = [dateFormatter dateFromString:str];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
NSLog(#"Time Diff - %f",timeDifference);
You can probably use plain C style code and get the difference as an integer and then decide what you need to return from the comparison function depending on wether the difference is positive or negative. You can compare minutes this way as well. Dont forget to import time.h.
time_t now = time(NULL);
struct tm oldCTime;
localtime_r(&now, &oldCTime);
int hours = oldCTime.tm_hour;
int diff = 17-hours;
NSLog(#"Time difference is: %d.", diff);
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];