I would like to collect a date from the user, convert it to unix epoch time and use that epoch time stamp to replace the time stamp that follows the dateWithTimeIntervalSince1970: in my code. Any assistance is greatly appreciated, thank you!
-(void)updatelabel{
NSCalendar *Calender = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [Calender components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c", [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']];
}
- (void)viewDidLoad {
[super viewDidLoad];
destinationDate = [NSDate dateWithTimeIntervalSince1970:1356088260];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatelabel) userInfo:nil repeats:YES];
}
If you are asking how to collect a date from a user, the easiest way is to use a UIDatePicker. Assuming you are using Storyboards or NIBs, you’d drag the control from the Object Library and connect its “Value Changed” event with an action method.
Inside your action method, you could grab the date as:
If, instead, you have an NSString from the user, you can use an NSDateFormatter to convert the string to an NSDate:
Either way, once you have a date object, if needed, you can convert it to an NSTimeInterval with: