Is there an easy way to determine Day X - 2 Working days in Delphi ?

Is there an easy way to determine Day X - 2 Working days in Delphi ?

I'm quite sure I've seen a routine for that at some point, but I can't seem to find it back. And since we now have a Delphi Developers community on Google+, I thought this might be a good place to ask the question.

In our case users plan a job on a given date. Planning a job means our WMS needs to pick some goods from the warehouse 2 working days in advance so everything is ready for delivery on the plan date. 

Does anyone know an easy way to get a Date - 2 working days ? I know I could subtract 2 days from the day and check if that's a weekday and continue to subtract 1 day. But that doesn't take into account any bank hollidays or such.

All suggestions are welcome of course.

Comments

  1. Are national holidays included? If yes, you might need a DB. Do you need to account for unplanned holidays. You might need to contact your suppliers and provide you with a timeschedule they use (some holidays may be industry-specific).

    That could make a good question to StackOverflow btw ;-)

    ReplyDelete
  2. Well ... we need to take into account our National Hollidays here in Belgium, and some other days on which the warehouse will be closed. For the last one I will surely need a DB setup where we can mark certain dates as 'non working days', but I was hoping I could start with some routines for just the National Hollidays.

    ReplyDelete
  3. something like this: date:= now(); numDays := 2; while ( numDays >0) do begin  date := date -day; if not isHoliday(date) then dec(numDate); end;

    ReplyDelete
  4. Well ... I was wondering how the IsHoliday( ) funciton would look like.

    ReplyDelete
  5. Use DayOfWeek() in combination with stored in DB country holidays dates, i suppose

    ReplyDelete
  6. Isn't there an internet service you could source holiday data from?

    ReplyDelete
  7. Although I've recently noticed some fairly extensive date math support in the JEDI library, we've been using this older "adrock" library to do exactly swhat you're asking for.  It has functions like "IsBusinessDay" and "IsHoliday", with support for supplying your own stringlist of holidays.  And because it is open source, you can easily customize it to your needs.   

    http://www.adrock.com/Products/DelphiComponents.aspx

    ReplyDelete
  8. I remember having an .ini file with holidays per year.  Typically, we always forgot to update it before the next year.  Fun with dates like Easter, which moves around every year.

    ReplyDelete
  9. For some strange reason I thought you could calculate all those special dates. I guess some are indeed specific to a country though.

    ReplyDelete
  10. Some dates are not only specific to a country (such as Thanksgiving, in USA and Canada -- in different months) but also subject to occasional relocation from absolute day to nearest Monday, as was done for some in the USA some years ago.

    ReplyDelete
  11. May be Google Calendar API can help you.

    ReplyDelete
  12. It seems that "Day X - 2 working days" should become very easy as soon as you figure out the "working days" part. ;-)

    ReplyDelete
  13. ProcessingDate := DeliveryDate;
    LeadDays := 2;
    repeat
      ProcessingDate := ProcessingDate - 1;
      if IsWorkingDay(ProcessingDate)
       then dec(LeadDays);
    until (LeadDays = 0);

    ReplyDelete
  14. You simply write a class that loads the holidays. You think there's one that works for all countries out there right now? No chance. Just write it.  100 lines of code tops.  Days of week is easy. Holidays is just a table, although it's not the same dates each year.   Easter for example is calculated by a solunar formula.  In some jurisdictions Roman Catholic dates for Easter may be statutory holidays, in other jurisdictions the Orthodox dates for Easter based on a non-reformed Julian calendar may be in use. Easiest way is just to pull the list from somewhere on the internet, and store it in your data files or database.  As years go by, statutory holidays often change in various places. I know that where I live, local provincial statutory holidays and semi-statutory holidays exist, and each business is closed different subset of them. give up now on trying to hardcode anything.

    ReplyDelete

Post a Comment