I grab the current time
NSDate * now = [[NSDate alloc] init];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents * comps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
And I can set the DatePicker to any values I desire
NSDate * date = [cal dateFromComponents:comps];
[self.off setDate:date animated:TRUE];
But how do I add a, say, 5 minutes to "now"?
Try this
NSDate *date = [[NSDate date] dateByAddingTimeInterval:5*60];
Try
NSDate *now = [NSDate dateWithTimeInterval:300 sinceDate:[NSDate date]]
Related
I have 2 button, one for increase the current date and one for decrease the date, so can you tell me how can I manage it I have using to show the current date in textfield by this way:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"dd/MM/YYYY"];
date_String=[dateformate stringFromDate:[NSDate date]];
_dateTF.text = date_String;
NSDate *decreaseDate= [NSDate dateWithTimeInterval:-60 * 60 * 24 sinceDate:<somedate>];
NSDate *inceraseDate= [NSDate dateWithTimeInterval:60 * 60 * 24 sinceDate:<somedate>];
The above code snippet will helps you to get the previous and next date.
You can use this code,
//This code will give you next date from current date
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1]; // you can change this as per your requirement, this will increase +1 date
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
// This code will give previous date from current date
NSDate *today1 = [[NSDate alloc] init];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents1 = [[NSDateComponents alloc] init];
[offsetComponents1 setDay:-1]; // you can change this as per your requirement, this will decrease -1 date
NSDate *previousDate = [gregorian1 dateByAddingComponents:offsetComponents1 toDate:today1 options:0];
You can change date formate, with you desire date format. Hope this helps.
Happy coding.
iOS Programming: I just want to let iOS7 get the "America/Chicago" current date and time.
I searched a lot on Internet, but there are a lot of different solutions. I tried several solutions, but they do not work.
You can use the NSCalendar and NSTimezone classes. The following code segment demonstrates retrieving the current hour and minute. Extending it to retrieve other date components is straight-forward -
long hour;
long minute;
NSCalendar *cal=[NSCalendar currentCalendar];
NSDate *now=[NSDate date];
NSTimeZone *tz=[NSTimeZone timeZoneWithName:#"America/Chicago"];
[cal setTimeZone:tz];
NSDateComponents *comp=[cal components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:now];
hour=[comp hour];
min=[comp minute];
You can use this method,
- (NSDate *) getCountryDateWithTimeZone:(NSString *)zone
{
NSDate *now = [NSDate date];
NSTimeZone *szone = [NSTimeZone timeZoneWithName:zone];
NSInteger sourceGMTOffset = [szone secondsFromGMTForDate:now];
NSTimeInterval interval = sourceGMTOffset;
NSDate *destinationDate = [NSDate dateWithTimeInterval:interval sinceDate:now];
return destinationDate;
}
And,
[self getCountryDateWithTimeZone:#"America/Chicago"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *firstDate = #"2014-06-05 12:55:00";
NSDate *date=[dateFormatter dateFromString:firstDate];
NSDate *currentDate = [NSDate date];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:date toDate:currentDate options:0];
NSLog(#"The difference between from date and to date is %d days and %d hours and %d minute and %d second",components.day,components.hour,components.minute,components.second);
Output:
DateBookApp[1179:70b] The difference between from date and to date is 0 days and 22 hours and 57 minute and 12 Second
I am using following piece of code to get next week date from a given date at 12:00am:
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit |NSHourCalendarUnit fromDate:now];
[components setHour:0];
NSDate *todayDate = [calendar dateFromComponents:components];
int daysToAdd = 7;
NSDate *tomorrowDate = [todayDate dateByAddingTimeInterval:60*60*24*daysToAdd];
NSString *stringDate = [NSString stringWithFormat:#"%#",tomorrowDate];
todoItemDueDateText.text = stringDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEEE, dd MMMM YYYY"];
NSString *fechasimulada = [dateFormatter stringFromDate:tomorrowDate];
dateFieldSimulatedText.text = fechasimulada;
If I log the result date, it is working fine.
Now, a similar piece of code to get next month date from a given date at 12:00am:
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:0];
NSDate *todayDate = [calendar dateFromComponents:components];
//NSDate *today = [NSDate date];
NSDateComponents* dateComponents = [[NSDateComponents alloc]init];
[dateComponents setMonth:1];
//NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* tomorrowDate = [calendar dateByAddingComponents:dateComponents toDate:todayDate options:0];
NSString *stringDate = [NSString stringWithFormat:#"%#",tomorrowDate];
todoItemDueDateText.text = stringDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEEE, dd MMMM YYYY"];
NSString *fechasimulada = [dateFormatter stringFromDate:tomorrowDate];
I don't know why, but the second piece of code gives the expected date but -1 hour.
UPDATE:
I will show you the dates:
Source date (today at 12:00am)+7 h
Next week date from previous given date:
Next month date from previous given date:
NSDate takes Daylight Savings into account. If you cross a DST day, you'll experience this issue.
I want to get the current & next week dates with day name.
i.e. If today's date is 28-06-2013 (Friday) then I want to get the dates from 23-06-2013(sunday) to 29-06-2013 (Saturday) as the this current week.
for next week
I want to get dates from 30-06-2013(sunday) to 06-07-2013 (saturday).
How can I do this?
Thank you,
Here is sample code that does the trick.
-(NSArray*)daysThisWeek
{
return [self daysInWeek:0 fromDate:[NSDate date]];
}
-(NSArray*)daysNextWeek
{
return [self daysInWeek:1 fromDate:[NSDate date]];
}
-(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
//ask for current week
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date];
//create date on week start
NSDate* weekstart=[calendar dateFromComponents:comps];
if (weekOffset>0) {
NSDateComponents* moveWeeks=[[NSDateComponents alloc] init];
moveWeeks.week=1;
weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0];
}
//add 7 days
NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
for (int i=1; i<=7; i++) {
NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
compsToAdd.day=i;
NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
[week addObject:nextDate];
}
return [NSArray arrayWithArray:week];
}
I was going to copy and paste an answer, but I'd suggest going and reading this post: Current Week Start and End Date. It looks like it'll help you achieve what you want to.
Something like this perhaps:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:now] week];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeek:weekNumber]; //Week number.
int i;
for (i = 1; i < 8; i=i+1){
[comp setWeekday:i]; //First day of the week. Change it to 7 to get the last date of the week
NSDate *resultDate = [gregorian dateFromComponents:comp];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE MMMM d, yyyy"];
NSString *myDateString = [formatter stringFromDate:resultDate];
NSLog(#"%#",myDateString);
}
Use NSDateComponents to find out the day of the week and then to find the date required before and after it.
Umm, I'm not an expert on this or anything, but a dumb way to do this is to first be able to recognize the day of the week from NSDate. Then you could add in if statements then the if statements could add and subtract the dates and nslog or printf each of them. Or I think there is a way by accessing the ios calendar.
okay so the above pretty much said and did more than what I said. Yeah go check the link on the post before me.
Use an NSCalendar instance to convert your start NSDate instance into an NSDateComponents instance, add 1 day to the NSDateComponents and use the NSCalendar to convert that back to an NSDate instance. Repeat the last two steps until you reach your end date.
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *nextDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
Or else find start date and end date. like
NSDate *startDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:7];
NSDate *endDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
then,
NSDate nextDate;
for ( nextDate = startDate ; [nextDate compare:endDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) {
// use date
}
I know how to get the date in iOS. I need to find last weeks date. So for example if today is 05/27/2011, I need to be able to get 05/20/2011
Thanks
This is crude but would work:
NSDate *prevWeekDate = [currentDate dateByAddingTimeInterval:(-60 * 60 * 24 * 7)];
Click for detail on dateByAddingTimeInterval:
How about:
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * date = [NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeek:-1];
NSDate * lastweek = [calendar dateByAddingComponents:components toDate:date options:0];
NSLog(#"%#", lastweek);
[components release];