LDOM - Last Day of Month

H

hunt4grouse

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
 
D

Dave Vandervies

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
 
A

Alan Balmer

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.
 
E

Eric Sosman

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().
 
K

Keith Thompson

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).
 
S

Stan Milam

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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top