I'm trying to check if two dates are 1 month apart. I read that I should use this method components:fromDate:toDate:options: from NSCalendar
My code
#define FORMAT_DATE_TIME #"dd-MM-yyyy HH:mm"
NSDate *updateDate = [Utils getDateFromString:airport.updateOn withFormat:FORMAT_DATE_TIME];
NSDate *now = [NSDate date];
NSString *nowString = [Utils getStringFromDate:now withFormat:FORMAT_DATE_TIME];
now = [Utils getDateFromString:nowString withFormat:FORMAT_DATE_TIME];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [calendar components:NSCalendarUnitMonth fromDate:updateDate toDate:now options:0];
NSLog(#"%ld", (long)[comps year]);
NSLog(#"%ld", (long)[comps month]);
NSLog(#"%ld", (long)[comps day]);
These are the logs:
(lldb) Year: 9223372036854775807
(lldb) Month: 0
(lldb) Day: 9223372036854775807
(lldb) po now 2017-08-23 11:54:00 +0000
(lldb) po updateDate 2017-08-23 11:48:00 +0000
Shouldn't all the value be zero?
The below line does not include the Year (NSCalendarUnitYear) and Day (NSCalendarUnitDay) component, only Month (NSCalendarUnitMonth) component is included.
NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:updateDate toDate:now options:0];
Add NSCalendarUnitYear and NSCalendarUnitDay to get [comps year] & [comps day] as 0.
Like this :
NSDateComponents * components = [calendar components:NSCalendarUnitMonth |NSCalendarUnitYear|NSCalendarUnitDay fromDate:updateDate toDate:now options:0];
you can use this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:FORMAT_DATE_TIME];
NSDate *updateDate = [dateFormatter dateFromString:#"24-07-1990 15:20"];
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *comps = [calendar components:unitFlags fromDate:updateDate toDate:now options:0];
NSLog(#"%ld", (long)[comps year]);
NSLog(#"%ld", (long)[comps month]);
NSLog(#"%ld", (long)[comps day]);
Related
I want to take absolute value from NSDateComponents.
<NSDateComponents: 0x600000477c80>
Calendar Year: -1
I tried the following way.But I did not get the exact value.How can i get the value 1 from the components.
NSDate *startDate = [formatter dateFromString:birthDate];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitYear
fromDate:startDate
toDate:today
options:0];
NSInteger year = [components year];
If you want to get difference of the dates in the years, then you have to use method : calendar: fromDate: toDate:
And if you want to get only year from the date, then you have to use this method : calendar: fromDate:.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSDate *today = [NSDate date];
NSString *birthDate = #"04/06/1984 01:45:20";
NSDate *startDate = [formatter dateFromString:birthDate];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitYear fromDate:startDate toDate:today options:0]; //Option 1
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitYear fromDate:startDate]; // Option 2
NSInteger year = [components year];
NSLog(#"Years : %ld", year);
If you use method of option 1, then it will print Years : 35.
And if you use method of option 2, then it will give Years : 1984.
I have an NSDate object *myDate.I can get the week day from this date and I want to set the weekday to an integer c and get the new date object in that week only.
I searched for it and I found the answer,here is the code..
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSLog(#"new date==%#",newDate);
This will return date of the next day.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MMM dd, yyy"];
date = [formatter dateFromString:string];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger day = [components day];
NSInteger weekday = [components weekday]; // if necessary
I have a datepicker and I want to have just one row with 3 options: current day, one day after and one day before .
Would you help me to do that?
My question is, how can I add 'current day' with one day after and one day before ?
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *end = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
Where days is an integer representing the days you want to add/subtract from the current day.
This is how i did-
-(void)getDate{
NSString *str_CurDate =[self getCurrentDate];
NSString *str_NextDay =[self getFormattedDate:[self dateByAddingDays:1]];
NSString *str_PrevDay =[self getFormattedDate:[self dateByAddingDays:-1]];
}
Now implement these method
-(NSString*)getCurrentDate{
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d/M/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
return dateString;
}
- (NSDate *) dateByAddingDays:(int)days {
NSDate *retVal;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
retVal= [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
return retVal;
}
-(NSString*)getFormattedDate:(NSDate*)myDate{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d/M/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
Encapsulate in a category on NSDate
- (NSDate *) dateByAddingDays:(int)days {
NSDate *retVal;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
retVal = [gregorian dateByAddingComponents:components toDate:self options:0];
return retVal;
}
How can I get a past date in objective C? Like,
NSDate *pastDate = //some date 2 weeks ago
Off of top of my head:
NSDate *today = [NSDate date];
NSDateComponents *dc = [[[NSDateComponents alloc] init] autorelease];
[dc setDay:-14];
NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *pastDate = [cal dateByAddingComponents:dc toDate:today options:0];
NSDate *today = [NSDate date];
NSLog(#"today: %#",today);
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setWeek:-2];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *pastDate = [currentCalendar dateByAddingComponents:dateComponents toDate:today options:0];
NSLog(#"pastDate: %#",pastDate);
NSLog output:
today: 2011-12-07 11:52:15 +0000
pastDate: 2011-11-23 11:52:15 +0000
This example used ARC.
Assume you don't care about DST,
NSDate* pastDate = [NSDate dateWithTimeIntervalSinceNow:-2*7*24*60*60];
// Get the date which is exactly 2 weeks (14 days) ago.
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009)
Thank you.
I've been doing:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEEE"];
NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]];
This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call
Lots more information at:
http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369
You may have a look to the NSDate and NSCalendar classes.
For example, here and here
They provide the following code:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
Use NSCalendar and NSDateComponents. As shown in the NSDateComponents documentation:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
Here's what I wound up with, which works exactly as intended. It takes a date, alters the date to be the first of the month, and gets the day of that date:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSDateComponents *monthDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate: date];
// build date as start of month
monthDateComponents.year = components.year;
monthDateComponents.month = components.month;
monthDateComponents.day = 1;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// [gregorian setFirstWeekday:1];
NSDate *builtDate =[gregorian dateFromComponents: monthDateComponents];
// NSDateComponents *weekdayComponents =[gregorian components: NSCalendarUnitWeekday fromDate: builtDate];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"E"];
NSString *firstDay = [df stringFromDate:builtDate];
if([firstDay isEqual:#"Sun"]) // do for the other 6 days as appropriate
return 7;