How to get the current date in Cocoa

I'm starting to develop for the iPhone, and so I look at various tutorials online, and also try different things. I'm currently trying to create a countdown to midnight. To get the number of hours, 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, in every place where 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') 

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

In addition, I noticed in different places and at different times, an asterisk (*) is either immediately after the NSDate* now , or right before the NSDate *now variable. What is the difference in two and why are you using one against the other?

+55
objective-c cocoa nsdate
Jul 01 '09 at 17:25
source share
14 answers

You should 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]; 

Now there is no difference between NSDate * now and NSDate *, it's just a matter of preference. From the point of view of the compiler, nothing changes.

+47
Jul 01 '09 at 17:37
source share

Having 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); 

-> 01/20/2011 10:36:02

+57
Jan 20 2018-11-11T00:
source share

You can also use:

 CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02.0f", currentDate.hour, currentDate.minute, currentDate.second]; CFRelease(currentDate); // Don't forget this! VERY important 

I think this has the following advantages:

  1. No direct memory allocation.
  2. Seconds are a double, not an integer.
  3. No message calls.
  4. Faster.
+21
Jul 01 '09 at 20:16
source share

// you can get the current date / time with the following code:

  NSDate* date = [NSDate date]; NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone]; formatter.timeZone = destinationTimeZone; [formatter setDateStyle:NSDateFormatterLongStyle]; [formatter setDateFormat:@"MM/dd/yyyy hh:mma"]; NSString* dateString = [formatter stringFromDate:date]; 
+12
Dec 27 '12 at 12:47
source share

Replace this:

 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]; 

Wherein:

 NSDate* now = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now]; NSInteger hour = [dateComponents hour]; NSInteger minute = [dateComponents minute]; NSInteger second = [dateComponents second]; [gregorian release]; countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second]; 
+5
Sep 22 '09 at 19:21
source share

In addition, I noticed in different places, and at different times, an asterisk ( * ) is located either immediately after the NSDate* now , or right before the NSDate *now variable. What is the difference in two and why are you using one against the other?

The compiler doesn't care, but put an asterisk before space can be misleading. Here is my example:

 int* a, b; 

What is type b ?

If you guessed int * , you're wrong. It is just an int .

Another way makes this a bit clearer by storing * next to the variable to which it belongs:

 int *a, b; 

Of course, there are two ways that are even clearer:

 int b, *a; int *a; int b; 
+4
Jul 02 '09 at 2:35
source share

You need to do something in the following lines:

 NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:now]; NSLog(@"%d", [components hour]); 

And so on.

+3
Jul 01 '09 at 17:45
source share

NSDate* now and NSDate *now are the same thing: a pointer to an NSDate object.

You probably want to use descriptionWithCalendarFormat: timeZone: locale: rather than dateWithCalendarFormat: - the latter returns an NSCalendarDate, which is said to be said to be deprecated at some point.

+2
Jul 01 '09 at 17:39
source share

Here's another way:

 NSDate *now = [NSDate date]; //maybe not 100% approved, but it works in English. You could localize if necessary NSDate *midnight = [NSDate dateWithNaturalLanguageString:@"midnight tomorrow"]; //num of seconds between mid and now NSTimeInterval timeInt = [midnight timeIntervalSinceDate:now]; int hours = (int) timeInt/3600; int minutes = ((int) timeInt % 3600) / 60; int seconds = (int) timeInt % 60; 

You lose subsecond precision with casting NSTimeInterval to int, but that doesn't matter.

+1
Jul 01 '09 at 19:36
source share

The original poster - how you define seconds until midnight, will not work on the day when summer time begins or ends. Here is a code snippet that shows how to do this ... It will be in the number of seconds (NSTimeInterval); you can do the unit / module / etc to get to what you need.

 NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]]; [dc setDay:dc.day + 1]; NSDate *midnightDate = [[NSCalendar currentCalendar] dateFromComponents:dc]; NSLog(@"Now: %@, Tonight Midnight: %@, Hours until midnight: %.1f", [NSDate date], midnightDate, [midnightDate timeIntervalSinceDate:[NSDate date]] / 60.0 / 60.0); 
+1
Sep 07 '11 at 11:40
source share

There is no difference in the location of the asterisk (on the C on which Obj-C is based, it does not matter). This is pure preference (style).

0
Jul 01 '09 at 17:32
source share

It took me a while to find out why the sample application works, but I donโ€™t.

The library (Foundation.Framework) referenced by the author is a system library (from the OS) where iphone sdk (I use 3.0) no longer supports it.

Therefore, an example application (from about.com, http://www.appsamuck.com/day1.html ) works, but we donโ€™t.

0
Aug 15 '09 at 14:05
source share
 CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%2.0f", currentDate.hour, currentDate.minute, currentDate.second]; 

Mark was right, this code is MUCH more efficient for managing the dates of hours min and secs. But he forgot @ at the beginning of the format string declaration.

0
Apr 08 '10 at 15:55
source share
 + (NSString *)displayCurrentTimeWithAMPM { NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; [outputFormatter setDateFormat:@"h:mm aa"]; NSString *dateTime = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]]; return dateTime; } 

return 3:33 a.m.

0
Dec 17 '12 at 19:32
source share



All Articles