Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:46:05+00:00 2026-05-22T19:46:05+00:00

I’ve come across a small issue that I’ve been chewing on for a day

  • 0

I’ve come across a small issue that I’ve been chewing on for a day or two now.

Using the Apple example project called DateCell I lifted its UIDatePicker and set it to time. I used this particular code as it did the animated slide on/off the screen.

My workflow is to set four values, start time, lunch time out, lunch time in, and stop time. I set these values by using [NSDate date].

I then use NSCalander:component calls to do some math such as “add 30 minutes to start time to get lunch time out,” and “start time – lunch time out – 8 hours to get stop time.”

The initial setup goes just fine. Clicking on the start time cell brings up the picker, and selecting a time change the other three times following my simple math formula’s.

If the second row is selected, the lunch time out, the wheel comes up again to pick your lunch time out time. However this is where my problems start. It seems that my UIDatePickerModeTime wheel returns a date portion for January 1, 1970. And its this date portion that messes up my math formulas.

My question is, what can I do to fix this?

I’ve tried setting an initial, minimum time, in the XIB for the picker. This sort of works but when you pick a time on the wheel, the wheel spins itself to the time set in the XIB. This method doesn’t have a clean feel to it.

I’ve tried settings the initWithTimeInterval class methods, but these block out times and isn’t what I’m looking for I think.

I’ve also tried the NSDateFormatter:stringFromDate|dateFromString calls, and these had no affect.

What I have not done yet:

Custom defined date/time string.

Rebuilding UIDatePicker:Time from scratch

Am I over looking anything?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:46:05+00:00Added an answer on May 22, 2026 at 7:46 pm

    I’ve solved my problem, and here is how I addressed it.

    Before I get to my answer I’m still really new to Objective-C and object oriented programming so my vocabulary doesn’t know how to describe some of the things I’ve tried explaining. So take this into account when reading this.

    Using UIDatePicker in time mode, i.e. you go into your NIB/XIB file and set your UIDatePicker object to ‘time’, will only return time. This is where I went wrong.

    Using the NSDateComponent or any of the NSCalendar methods will bring out the date component of the picker. Thus you’ll see January 1st, 1970 in the NSLog returns for example.

    I had to find a new way of doing my math and manipulation of the times I was getting from the picker.

    What I ended up using is NSTimeInterval, dateByAddingTimeInterval, and timeIntervalSinceDate. Research showed that NSTimeInterval is also a float type, so I used float to do some math as well.

    Here an example –

    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Clock Out"])
        {        
            NSTimeInterval diffClockInOutToLunch = [outToLunch timeIntervalSinceDate:clockIn];
            float remainingInTheDay = 28800 - diffClockInOutToLunch;
    
            self.clockOut = [inFromLunch dateByAddingTimeInterval:remainingInTheDay];
    
            cell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockOut];
    
            self.clockOutIndex = indexPath;
    
            return cell;
        }
    

    I’m using a TableView to display my fields. When this ‘if’ statement is tripped it will populate the detailTextLabel of the line displaying “Clock Out.” Visually the phrase “Clock Out” will be on the left side of the row, the time will be on the right side.

    diffClockInOutToLunch is defined as a NSTimeInterval type. The operation being performed is timeIntervalSinceDate which essentially subtracts the value of outToLunch from the value of clockIn. Imagine outToLunch as being 11:00pm and clockIn as being 6:00am. This difference is 5 hours. NSTimeInterval stores values as seconds only so this difference of 5 hours is 18000 seconds.

    I then perform a normal math operation using float. In this case I want to find out how many hours remain in the work day. This assumes the hours worked in a day is 8 hours. Because NSTimeInterval returns seconds, I converted 8 hours into seconds (28,800 seconds) and then subtract diffClockInOutToLunch from 28800. Now remainingInTheDay is equal to to 10800, or 3 hours.

    The next operation I perform is set clockOut to the time our work day is finished. To do this I use the dateByAddingTimeInterval operation, which also is a NSDate method, so whatever it returns will be in a date/time format. In this operation we add remainingInTheDay (10,800 seconds) to inFromLunch (11:30am for example). Our clockOut time is now 2:30pm which is then sent through my DateFormatter and returned as a string to the cell of the TableView and also stored for later use.

    Here’s another example, from further down in my code –

    - (void)clockInChanged
    {
        // Set clockIn value
        self.clockIn = self.pickerView.date;
    
        // Change the outToLunch time
        self.outToLunch = [self.pickerView.date dateByAddingTimeInterval:5*60*60];
    
        UITableViewCell *outToLunchCell = [self.tableView cellForRowAtIndexPath:outToLunchIndex];
        outToLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];
    
        // Change the inFromLunch time
        self.inFromLunch = [outToLunch dateByAddingTimeInterval:30*60];
    
        UITableViewCell *inFromLunchCell = [self.tableView cellForRowAtIndexPath:inFromLunchIndex];
        inFromLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:inFromLunch];
    
        // Change the clockOut time
        NSTimeInterval diffClockInOutToLunch = [outToLunch timeIntervalSinceDate:clockIn];
        float remainingInTheDay = 28800 - diffClockInOutToLunch;
    
        self.clockOut = [inFromLunch dateByAddingTimeInterval:remainingInTheDay];
        UITableViewCell *clockOutCell = [self.tableView cellForRowAtIndexPath:clockOutIndex];
        clockOutCell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockOut];
    } 
    

    In this example, we’ve previously determined that the row pertaining to “Clock In” time was selected (“Touch Up Inside” if you will) and we drop to this method.

    What happens in this method is whenever clockIn is changed using the picker, the times displayed in outToLunch, inFromLunch, and clockOut automatically update and are displayed.

    This example shows that we capture the value on the picker (self.pickerView.date) as clockIn. We then use clockIn to seed our mess of dateByAddingTimeInterval’s and so forth.

    So. This is how I managed my times using UIDatePicker (which is set to time mode).

    The short answer would be I was using the wrong methods to work with what my picker was turning.

    I hope this helps you and hopefully it’ll be here if I need it again too 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.