Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current date in Cocoa

I'm getting started developing for the iPhone and as such I am looking at different tutorials online as well as trying some different things out myself. Currently, I'm trying to create a countdown until midnight. To get the number of hour, minutes, and seconds, I do the following (which I found somewhere):

NSDate* now = [NSDate date];  int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay]; int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour]; int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute]; countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec]; 

However, each place I use -dateWithCalendarFormat:timeZone: I get the following error:

warning: 'NSDate' may not respond to '-dateWithCalendarFormat:timeZone:' (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.) warning: no '-hourOfDay' method found error: invalid operands to binary - (have 'int' and 'id') 

This seems like something very simple. What am I missing?

Also, I've noticed at different places and at different times the asterisk (*) is located either right after the time NSDate* now or right before the variable NSDate *now. What is the difference in the two and why would you use one versus the other?

like image 271
Jason Avatar asked Jul 01 '09 17:07

Jason


People also ask

How do I get the current date in Objective C?

dateFormat = @"MMMM dd, yyyy"; NSString* dateString = [formatter stringFromDate:date]; Convert a String to a Date: NSDateFormatter* formatter = [[NSDateFormatter alloc]init];

What is NS date?

Overview. NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

How do I get todays date in Swift?

Get current time in “YYYY-MM–DD HH:MM:SS +TIMEZONE” format in Swift. This is the easiest way to show the current date-time.

How do you initialize a date in Swift?

This is the easiest way to create a date object. Now we will be seeing a second way to create the data object i.e. by using Date Formatter. let stringDate = "2019-10-10" let dateFormatter = DateFormatter() dateFormatter. dateFormat = "yyyy-MM-dd" let date = dateFormatter.


2 Answers

You have problems with iOS 4.2? Use this Code:

NSDate *currDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:currDate]; NSLog(@"%@",dateString); 

-->20.01.2011 10:36:02

like image 139
Hover Avatar answered Oct 07 '22 09:10

Hover


You must use the following:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourDateHere]; NSInteger hour = [dateComponents hour]; NSInteger minute = [dateComponents minute]; NSInteger second = [dateComponents second]; [gregorian release]; 

There is no difference between NSDate* now and NSDate *now, it's just a matter of preference. From the compiler perspective, nothing changes.

like image 36
Massimo Cafaro Avatar answered Oct 07 '22 08:10

Massimo Cafaro