469,611 Members | 1,714 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,611 developers. It's quick & easy.

LDOM - Last Day of Month

I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28

Nov 14 '05 #1
5 6075
In article <11**********************@g14g2000cwa.googlegroups .com>,
<hu*********@yahoo.com> wrote:
I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28


Here's the easy part:
--------
int ndays[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};

int get_last_day(int month,int year)
{
int ret=ndays[month];
if(is_leap(year) && month==2)
ret++;
return ret;
}
--------
Extracting the month and year from your input, implementing is_leap,
and packing the last day (along with the month and year) into the output
format are left as exercises.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
What you have in mind about trees is extremely obscure. They come in green,
red-black, avl, binary, B, deciduous, non-deciduous, tropical, fruit,
ornamental, just to name a few varieties. --CBFalconer in comp.lang.c
Nov 14 '05 #2
On 25 Feb 2005 11:55:03 -0800, hu*********@yahoo.com wrote:
I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28


This is so trivial that it must be homework. We don't do homework.
Have a go at it, then if you have problems, post your code here.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #3


hu*********@yahoo.com wrote:
I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28


There's no built-in function to do this. However,
you could use the fact that the mktime() function accepts
non-canonical inputs and "normalizes" them, making multiple
calls to try out different possible end dates until you
find one that lands in the specified month:

int year = (input / 10000);
int month = (input / 100) % 100;
int day;
for (day = 31; ; --day) {
struct tm bdt = { 0 };
bdt.tm_year = year - 1900;
bdt.tm_mon = month - 1;
bdt.tm_mday = day;
mktime (&bdt);
if (bdt.tm_mon == month - 1)
break;
}
printf ("%d/%02d/%02d\n", year, month, day);

Alternatively, you could use a simple pre-initialized
array and make your own Leap Year determination. However,
a surprising number of programmers have botched the simple
Leap Year test, so it might be safer to stick with mktime().

--
Er*********@sun.com

Nov 14 '05 #4
hu*********@yahoo.com writes:
I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28


Is the input a string or an integer? If it's an integer, keep in mind
that there's no guarantee that type int can represent a value larger
than 32767.

If you have any influence over the output requirements, I encourage
you to use hyphens rather than slashes for separators:

2005-02-28

This is the ISO 8601 date format.

If this is homework, I encourage you to do it yourself (or give us
your instructor's e-mail address so we can submit the solution
directly).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
hu*********@yahoo.com wrote:
I'm looking for C code that will give me the last day of the month.

Input - any day that I pass in as a variable....
input = 20050201

Output - calculate and format with slashes....
output = 2005/02/28

From my date library documentations:

---------------------------------------------------------------
last_day_of_month()
---------------------------------------------------------------
NAME:
last_day_of_month() - Computes a date value for the last day
of the month.

SYNOPSIS:
#include "datetool.h" or "toolbox.h"
date_t last_day_of_month( date_t date_value );

DESCRIPTION:
The last_day_of_month() function uses the date_value argument to
compute a date value which is the last day of the month in which
the date_value argument falls. For example, if the date_value
argument were for Monday, 14 February 1994 the last_day_of_month()
function would calculate a date value for Monday, 28 February 1994.
The date_value argument is checked to be a valid date value between
01/01/0001 A.D. and 12/31/9999 A.D.

ARGUMENTS:
A valid date value between 01/01/0001 A.D. and 12/31/9999 A.D.

RETURN VALUE:
A date value representing the last day of month in which the
argument falls. A value of (date_t) -1L is returned when the
date_value argument is invalid.

SEE ALSO:
first_day_of_month(), date().

Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sasidhar Parvatham | last post: by
6 posts views Thread by Ante Perkovic | last post: by
4 posts views Thread by laredotornado | last post: by
reply views Thread by larry | last post: by
3 posts views Thread by ats | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.