The following code:
NSDate* date1 = [NSDate date];
NSDate* date2 = [NSDate date];
[date1 compare:date2];
[(NSDate*)[NSDate date] compare:date2];
[[NSDate date] compare:date2];
gives me "Incompatible pointer types sending 'NSDate *' to parameter of type 'NSNumber *'" for the last date comparison but not for the first two!
What is going on???
You need to cast to an NSDate because + (id)date returns an id, not an NSDate.
Related
I tried this :
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss TZD"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"Current Date : %#",dateString);
and the result I get is Date and Time but not the Timezone.
#Vaibhav you have to use time zone like this
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
refer this link
Get the time and date of selected time zone?
Ever time I try to make a string in xcode it won't work can someone point out what's wrong and help please
NSString *date = self.DatePicker.date;
This is the error "Incompatible pointer types initializing 'NSString * strong' with an expression of type 'NSDate *'"
self.DatePicker.date; returns date.
You are storing it in NSString.
Use:
NSDate *date = self.DatePicker.date;
Then if you want to store it in string then use:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
I have this subset of a method that needs to get day one of the current month.
NSDate *today = [NSDate date]; // returns correctly 28 february 2013
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000, the first day of the next month.
How do I get day one of the current month?
Your logic is wrong: Instead of setting the date's day to 1, you're adding a day to the current date.
Try something like that:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
components.day = 1;
NSDate *dayOneInCurrentMonth = [gregorian dateFromComponents:components];
You want [NSCalender dateFromComponents:] instead:
NSDate *dayOneInCurrentMonth = [gregorian dateFromComponents:components];
Easy way of getting to the 1st from a given date:
NSDate *first = [gregorian dateBySettingUnit:NSCalendarUnitDay value:1 ofDate:date options:0];
Easy Way to get first and last date of previous month is :
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:[NSDate date]];
//TO GET PREVIOUS MONTH LAST DAY
[comp setMonth:[comp month]];
[comp setDay:1];
NSDate *tDateMonth = [gregorian dateFromComponents:comp];
NSLog(#"LAST DAY OF PREVIOUS MONTH ; %#", tDateMonth);
//TO GET PREVIOUS MONTH FIRST DAY
[comp setMonth:[comp month]-1];
[comp setDay:3];
NSDate *td = [gregorian dateFromComponents:comp];
NSLog(#"FIRST DAY OF PREVIOUS MONTH ; %#", td);
Hope this helps
By creating new variable is expensive on memory usage, it's better using date formatter to get the first day of the current month
today = [NSDate date];
firstDayDateFormatter = [[NSDateFormatter alloc] init];
[firstDayDateFormatter setDateFormat:#"01-MM-yyyy"];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
firstDayOfTheMonth = [mdfDateFormat dateFromString:[firstDayDateFormatter stringFromDate:today]];
NSDate *startDate = nil;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:NULL forDate:date];
return startDate;
I want to assign nstimeInterval value to nsdate.
NSDate *startTime=[NSDate date] ;
NSDate *endTime=[NSDate date] ;
NSTimeInterval difference=[endTime
timeIntervalSinceDate:startTime];
startTime=difference; This gives me an error.
how can i assign my difference result of nsTimeInterval type to nsdate?
thanks in Advance
You need to understand that NSTimeInterval is the difference between two dates. What you're trying to do is like telling the program to:
Set the date to 2 days.
When what you want to do is tell the program to:
Set the date to last monday plus 2 days.
Therefore you need to use one of these family of NSDate methods:
+ dateWithTimeIntervalSinceNow:
+ dateWithTimeInterval:sinceDate:
+ dateWithTimeIntervalSinceReferenceDate:
+ dateWithTimeIntervalSince1970:
– initWithTimeIntervalSinceNow:
– initWithTimeInterval:sinceDate:
– initWithTimeIntervalSinceReferenceDate:
– initWithTimeIntervalSince1970:
Where you create the NSDate with some reference date plus/minus the NSTimeInterval; note that NSDate objects are immutable so there are no methods to change the date represented by the object using NSTimeInterval values.
See the NSDate Class Reference for more details.
To correct your code, this is how you'd do it:
NSDate *startTime=[NSDate date] ;
NSDate *endTime=[NSDate date] ;
NSTimeInterval difference=[endTime timeIntervalSinceDate:startTime];
NSDate *newTime = [NSDate dateWithTimeInterval:difference sinceDate:startDate];
How do I set the time of the DatePicker at 00:00:00 of the current date?
- (void) awakeFromNib
{
[datePicker setDateValue:[NSDate date]];
NSDate *now = [NSDate date];
int daysToAdd = 364;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
[datePicker1 setDateValue:newDate1];
}
cringe
It looks like you have two different datePickers? datePicker and datePicker1? What's up with that?
Also, this does not do what you're expecting:
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*364];
That is creating a new date that is exactly 31,449,600 seconds in the future. It is not doing anything other than that.
What you want to do is extract all of the date components from the current date and zero them out:
NSDate *now = [NSDate date];
NSDateComponents *nowComponents = [[NSCalendar currentCalendar] components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:now];
// technically these next lines are unnecessary, since we only pulled out the era-year-month-day, but they're included here for understanding/completeness:
[nowComponents setHour:0];
[nowComponents setMinute:0];
[nowComponents setSecond:0];
// now we can turn it back into a date:
NSDate *todayAtMidnight = [[NSCalendar currentCalendar] dateFromComponents:nowComponents];