Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Last Wokring Day

Posted on 1998-08-12
5
Medium Priority
?
232 Views
Last Modified: 2010-04-01
Hi,
  I want to get last working day.  
  For example, today is "12-August-98" and I want to get "11-August-98".  Also, if today is "17-august-98", then the program should print out "14-August-98".
Thanks!!
jpchen@yahoo.com
0
Comment
Question by:jpchen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1170112
If you have a library that converts the date into a julian date as well as returning the day of the week then you're all set. Assume TDate is such a class:

  TDate test("14-August-98");

  do
  {
    test = test.PrevDate();
  } while (!IsWorkingDate(test));


All TDate::PrevDate has to do is take the date and convert it to Julian date and subtract one.

IsWorkingDate() examines the day of the week the date is and returns TRUE if it is Monday through Friday, otherwise FALSE.
0
 
LVL 2

Expert Comment

by:kinkajou
ID: 1170114
What are your expectations jpchen? Do you want someone to write the code and send you an exe or just some code commented here? What platform do you need this for?
0
 

Author Comment

by:jpchen
ID: 1170115
I don't need the code and I just need a hit and a easy way to do this.
Anyway, I found the solution by my self this morning.

Thanks all!!
jpchen

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 600 total points
ID: 1170116
OK.  Here's how to do it.  Assuming you're using standard C library.

(1) Convert the date you're starting with (in your example 12 Aug or 17 Aug) into time since some reference point.
Using standard time.h header, you can do this by filling in struct tm and calling mk_time (which will return a value of type time_t which is seconds since 1970).  Keep this value

(2) Work out day of week.
Using standard C library convert back to a struct tm by calling gmtime (this gives a day of week 0-6).

(3) Depending on day of week, subtract from value generated in stage (1).  
If day of week is Monday subtract (60*60*24*3) - i.e. 3 days going back to previous Friday
If day of week is Tues to Sat, subtract (60*60*24*1) - i.e. 1 day going back to previous day
If day of week is Sunday, subtract (60*60*24*2), i.e. 2 days going back to Friday

(4) Convert the value from (3) back into a date.  Again you can use gmtime

One last point, this doesn't deal with holidays which of course are different in each country anyway (hmmm we don't celebrate July 4 or Thanksgiving in the UK <g>).  The only way to deal with this is to include a data table in your program to take these into account.

Finally, a request - if possible -  Please encourage your friends at yahoo to index my site ASAP (i've already submitted a while ago) - it's http://www.ans2000.com
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question