473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6835
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
3
by: Gol Yass | last post by:
Dear Newsgroup, Is there a function in Access XP to take a date or month , and return the last day in that month? Ex: the last day in september is 30 or the last day in May is 31 . Thank...
3
by: Melissa | last post by:
I have this table: TblProjectYear ProjectYearID ProjectYearStartDate ProjectYearEndDate The Project Year will always span across December 31; for example 9/1/04 to 6/30/05. How do I build a...
2
by: Sasidhar Parvatham | last post by:
Hello All, Is there a way I can get dates for first and last fridays of a month? Any suggestion would be helpful. Thanks.
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
4
by: laredotornado | last post by:
Hi, Using PHP 4, if I have a date, what is a function I could use to give me a date that represents the first day of that month? For example, if my date were "3/19/2006 8:00", I would want my...
0
by: larry | last post by:
I am in the process of rewriting one of my first PHP scripts, an event calendar, and wanted to share the code that is the core of the new calendar. My current/previous calendar processed data...
3
by: ats | last post by:
Does anybody have any sample code for calculating the date for teh last Friday in each month. TIA -- ats@jbex When an old lady got hit by a truck I saw the wicked gleam in your eyes
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.