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

  • SEARCH
  • Home
  • 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 6896139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:59:50+00:00 2026-05-27T06:59:50+00:00

Using today as an example, how do I determine which date it was, 230

  • 0

Using today as an example, how do I determine which date it was, 230 workdays ago?

I know how to do it iteratively with a while loop checking date and subtracting 1 if it’s a workday, but I’m wondering if there is a better method.

Also, let’s take a Sunday 1 PM as an example, and subtract 3 work days and 2 hours from that time. First, it doesn’t make sense to subtract work-time from weekends. So it would have to move the time to 23:59:59 of Friday, and then subtract those 3 days and 2 hours.

If it’s a Monday at 1:30 AM, and I’m subtracting 5 days and 3 work-hours from that time, then the result should be Friday 22:30 PM of the previous week.


Code to test Kevin’s method:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *dc = [[NSDateComponents new] autorelease];
dc.month = 12;
dc.day = 19;
dc.year = 2011;
dc.hour = 1;
dc.minute = 0;
dc.second = 0;

NSDate *date = [cal dateFromComponents:dc];

NSLog(@"%@", [date descriptionWithCalendarFormat:nil timeZone:nil locale:nil]);
date = dateBySubtractingWorkOffset(date, 0, 2);
NSLog(@"%@", [date descriptionWithCalendarFormat:nil timeZone:nil locale:nil]);

Output log:

2011-12-02 16:33:46.878 otest[7124:707] 2011-12-19 01:00:00 -0500
2011-12-02 16:33:47.659 otest[7124:707] 2011-12-18 23:00:00 -0500

It should never be 12-18, since that’s a Sunday.

  • 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-27T06:59:51+00:00Added an answer on May 27, 2026 at 6:59 am

    Figure out how long from the last weekend your date is, subtract that amount from both your date and your offset. Now you can divide your offset by 5 to figure out how many full weeks are in your offset, and then multiply that by 7 and subtract this new value from your date. Take your previous offset (the one you divided by 5) and mod it by 5, to get the number of remaining days. If it’s greater than 0, subtract that offset + 2 (for the weekend) from your date.

    Note, this assumes every single weekday is a workday. Corporate holidays tend to make that assumption invalid. If you need to handle holidays, you’re in for a much tougher problem.

    Update: Here’s an attempt to fix David’s code to actually express the idea here:

    NSDate *dateBySubtractingWorkOffset(NSDate *date, NSUInteger days, NSUInteger hours) {
        const int secsInHour = 60*60;
        const int secsInDay = 24*secsInHour;
        NSTimeInterval offset = days*secsInDay + hours*secsInHour;
        NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    
        // figure out distance from last weekend
        {
            NSUInteger units = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit;
            NSDateComponents *dc = [cal components:units fromDate:date];
            if (dc.weekday == 1 || dc.weekday == 7) {
                // we're in the weekend already. Let's just back up until friday
                // and then we can start our calculations there
            } else {
                // figure out our offset from sunday 23:59:59
                dc.day -= (dc.weekday - 1);
                dc.weekday = 1;
                dc.hour = 23;
                dc.minute = 23;
                dc.second = 23;
                NSDate *sunday = [cal dateFromComponents:dc];
                NSTimeInterval newOffset = [date timeIntervalSinceDate:sunday];
                if (offset < newOffset) {
                    // our offset doesn't even go back to sunday, we don't need any calculations
                    return [date dateByAddingTimeInterval:-offset];
                }
                offset -= [date timeIntervalSinceDate:sunday];
                // Now we can jump back to Friday with our new offset
            }
            // Calculate last friday at 23:59:59
            dc.day -= (dc.weekday % 7 + 1);
            dc.hour = 23;
            dc.minute = 59;
            dc.second = 59;
            date = [cal dateFromComponents:dc];
        }
    
        // We're now set to Friday 23:59:59
        // Lets figure out how many weeks we have
        int secsInWorkWeek = 5*secsInDay;
        NSInteger weeks = (NSInteger)trunc(offset / secsInWorkWeek);
        offset -= weeks*secsInWorkWeek;
        if (weeks > 0) {
            // subtract that many weeks from the date
            NSDateComponents *dc = [[NSDateComponents alloc] init];
            dc.week = -weeks;
            date = [cal dateByAddingComponents:dc toDate:date options:0];
            [dc release];
        }
        // now we can just subtract our remaining offset from the date
        return [date dateByAddingTimeInterval:-offset];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I had to fix some older VB.NET 1.0 code which is using threads.
I am defining monday and friday using the following: @monday = Date.today.at_beginning_of_week @friday =
When using NSDate to get today's date, I seem to be getting a date
before this i'm using this function to from 6 month date to today date..
I am using today's timetable plugin on my website. Problem is, starting time is
I tried backing up our site today using the Unix 'cp' command and ended
Today we're using a shared SQL Server database and that is perfect as I
Just today I've started using Drupal for a site I'm designing/developing. For my own
Today is my first day using ASP.NET MVC, and I'm finding it very intriguing.
Today I ran a bunch of doctests using Python 2.6 on a Ubuntu 9.10

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.