How to display Date,Month,Year,Hours and minutes in UIDatePicker in Objective C - uidatepicker

I need to display Date,Month,Year, Hours, Minutes and AM/PM in the same UIDatePicker.As of now when I set the datePickerMode as UIDatePickerModeDateAndTime it doesn't display year. I need year to be displayed with others. Can we do this by any datePicker modes or some other alternatives
This is the code I am using.
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, screenHeight-264, screenWidth/2, 200)];
self.datePicker.tag = ansSeq;
[self.datePicker addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[_dateText setInputView:self.datePicker];
Please guide me on this.Thank you very much in advance.

Related

How to calculate and display 4 other days from the day i add in date picker and set local notification to it?

In my App, When i select a day in the datepicker it should automatically calculate and display 4 other days and also it should notify on the corresponding days. For Eg. if i enter 19-08-2014, it should calculate and display the 3rd day, the 7th day, the 14th day and the 21st day from 19-08-2014(the day i entered in datepicker).
How would i achieve this. Any help would be greatly appreciated.
I have added my current code for your reference. This doesnt serve my purpose.
Kindly Help.
(IBAction)save:(UIButton *)sender {
NSDate *pickerDate = [self.picker date];
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
//localNotif.alertBody = _enterText.text;
localNotif.alertBody = #"Please Take Your Rabipur Dosage";
localNotif.fireDate = pickerDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.soundName = (UILocalNotificationDefaultSoundName);
localNotif.applicationIconBadgeNumber = 1;
//localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
[self dismissViewControllerAnimated:YES completion:nil];
}
Create an NSDateComponents object with the date difference you want to add, and add it to pickerDate via NSCalendar -dateByAddingComponents:toDate:options. Then create your notification based on the resulting date.
In this case, something like this:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:3];
NSDate *reminderDate = [gregorian dateByAddingComponents:offsetComponents toDate:pickerDate options:0];
Repeat for 7th, 14th, and 21st day.

UIPickerView Changing UIButton Title Size

So I'm making Alarm Clock to test my skills but can't seem to figure this out... What I've got a UIPickerView that gives a user a time that they can select. Once the time is selected the titleLabel on a UIButton is supposed to update with the time they selected and it does, but it shrinks the new time so it's unreadable... Is there something that needs adjusted with the formatting?
Before when my page loads
Here's my code
- (NSDate *)dateFromString:(NSString *)string {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"15:00"];
assert(date != nil); // Error converting string to date
return date;
}
After when a user has set a time
Any reason why it might be doing this?
You're experiencing middle truncation of the button's text, and there are a couple of cures for this. You can either make the button bigger by simply adjusting its frame. Or, you can adjust the text size of the title label:
[myButton.titleLabel setFont:[UIFont fontWithName:myButton.titleLabel.font.fontName size:10.0f]];
Actually this seemed to resolve the issue.
alarmButton.titleLabel.adjustsFontSizeToFitWidth = YES;

Using specific time and day (NOT date) to activate 'if' function in Xcode

I'm current piecing together a streaming radio app for a show I work on and so far, it's okay - the streaming works once you push the 'Listen Live!' button and since that's the main aim, I'm happy. However, I'm trying to get clever now and set up an on-screen display that shows when the show is on the air; it's only on for two hours a week, so I thought it'd be nice to show when it's on and off via an on-screen display in the app. Nothing fancy... here's what I've got on screen so far:
1) A counting clock taken from here - EDIT: http://www.youtube.com/watch?v=pNVWST15pyc – that's really simple. Can't believe I didn't realise that changing the hh to HH makes it 24-hour... god, I'm so dumb.
2) A label to show just the day of the week, not the date or month, taken from here - NSCalendar to Display Weekday - as the app only needs to know whether it's Sunday (the on-air day) or not. EDIT: Made this work properly now because I'm an idiot who didn't link it up properly. :(
What I really want to do, however, is have the app set up two settings for an image view - an 'on air' and 'off air' switch - so that when it's between 8pm and 10pm on Sunday night, it shows the 'on air' image and when it's any other time, it shows the 'off air' one. I'm pretty sure that's an if statement but I'm not sure where to start trying to combine the clock and weekday bits to make that work. Can anyone make some suggestions please?
Also, since those times are GMT, I want to lock the app into GMT regardless of where in the world the user is so it relates directly to the show. I'm guessing I can do that using the timeZone:nil bit of the clock code by changing the 'nil' bit to 'Europe/London', but doing so just makes the whole thing crash out in spectacular fashion (I've tried it in various forms with no success). Again, suggestions would be massively appreciated.
Apologies for asking what may seem like simple questions - aside from some adult learning courses on Xcode, I'm a bit of a novice. :D
You should use the NSDateComponents object, which has a -weekday instance method (1 being Sunday for the gregorian calendar). Details on how to get started are in Apple's Date and Time Programming Guide. Here's a code sample that relates to what you are trying to do:
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];
NSDateComponents will also let you get and set (through so-called accessor methods) hour, minute, second, and timezone.
For the problem you described above, I would write a method like this:
- (BOOL)isShowOnAir {
BOOL onAir = NO;
NSDate *now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSTimeZoneCalendarUnit) fromDate:now];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSInteger day = [components day]; // Sunday == 1
if (day == 1) {
NSInteger hour = [components hour];
//NSInteger minute = [components minute]; -- not used but here's how to access it
if (hour == 20 || hour == 21) { // covers 8:00 - 9:59 PM
onAir = YES;
}
}
return onAir;
}

Countdown Timer in Cocoa

I am wondering if there is some way that I can create a timer that countdown from a given time. For example, say I want this timer to last an hour. There will be a NSTextField that will show the time remaining (ex. 25 minutes), and will auto update every minute to the new value. And then, when an hour is finally passed, it will run some function. I have seen people suggesting NSTimer and NSDate for this, but am wondering what you all could suggest.
Thanks!
EDIT: My current code (timeInstance is an instance variable):
- (void)awakeFromNib:
{
timeInstance = [[NSDate date] addTimeInterval:(10 * 60)];
[timeInstance retain];
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(timer:) userInfo:NULL repeats:YES];
}
- (void)timer:(NSTimer *)myTimer
{
NSDate *now = [NSDate date];
// Compare
}
NSTimer and NSDate sounds perfectly reasonable.
EDIT: As a side note, it might be a good idea to increase the frequency as the target time approaches; allowing you to change from hour display to minute display to second display.

Is there a better way to find midnight tomorrow?

Is there a better way to do this?
-(NSDate *)getMidnightTommorow {
NSCalendarDate *now = [NSCalendarDate date];
NSCalendarDate *tomorrow = [now dateByAddingYears:0 months:0 days:1 hours:0 minutes:0 seconds:0];
return [NSCalendarDate dateWithYear:[tomorrow yearOfCommonEra]
month:[tomorrow monthOfYear]
day:[tomorrow dayOfMonth]
hour:0
minute:0
second:0
timeZone:[tomorrow timeZone]];
}
Note that I always want the next midnight, even if it happens to be midnight when I make that call, however if it happens to be 23:59:59, I of course want the midnight that is coming in one second.
The natural language functions seem flaky, and I'm not sure what Cocoa would do if I pass 32 in the "day" field. (If that'd work I could drop the [now dateByAddingYears:...] call)
From the documentation:
Use of NSCalendarDate strongly
discouraged. It is not deprecated yet,
however it may be in the next major OS
release after Mac OS X v10.5. For
calendrical calculations, you should
use suitable combinations of
NSCalendar, NSDate, and
NSDateComponents, as described in
Calendars in Dates and Times
Programming Topics for Cocoa.
Following that advice:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *tomorrow = [gregorian dateByAddingComponents:components toDate:today options:0];
[components release];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
components = [gregorian components:unitFlags fromDate:tomorrow];
components.hour = 0;
components.minute = 0;
NSDate *tomorrowMidnight = [gregorian dateFromComponents:components];
[gregorian release];
[components release];
(I'm not sure offhand if this is the most efficient implementation, but it should serve as a pointer in the right direction.)
Note: In theory you can reduce the amount of code here by allowing a date components object with values greater than the range of normal values for the component (e.g. simply adding 1 to the day component, which might result in its having a value of 32). However, although dateFromComponents: may tolerate out-of-bounds values, it's not guaranteed to. You're strongly encouraged not to rely on it.
Nope - it'll be the same way you use to find midnight today.
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:(24 * 60 * 60)];
NSDateComponents *components = [gregorian components:(NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit)
fromDate:tomorrow];
NSDate *midnight = [gregorian dateFromComponents:components];
[NSDate dateWithNaturalLanguageString:#"midnight tomorrow"];
Convert your current date and time to a Unix date (seconds since 1970) or DOS style (since 1980), then add 24 hours and convert it back. Then reset the hours, minutes and seconds to zero to get to midnight.
You could try this way:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *tomorrow = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]; //it gives us tomorrow with current time
NSDate *midnight = [calendar startOfDayForDate:tomorrow]; //here we get next midnight
It is also easy to retrieve the seconds interval if needed to set up an NSTimer:
double intervalToMidnight = midnight.timeIntervalSinceNow;

Resources