473,654 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ 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 6883
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
<hu*********@ya hoo.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,3 1,30,31,31,30,3 1,30,31};

int get_last_day(in t 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*********@yah oo.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*********@yah oo.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*********@yah oo.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_Keit h) 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*********@yah oo.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_mon th()
---------------------------------------------------------------
NAME:
last_day_of_mon th() - Computes a date value for the last day
of the month.

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

DESCRIPTION:
The last_day_of_mon th() 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_mon th()
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_mo nth(), date().

Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3595
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 sure if it can be done, but all help welcomed. E.g. I have December and would like to see the last 2 weeks.. So this doesnt mean the last 15 days. What i mean by this is...
3
63108
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 you very much, Gol
3
6609
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 combobox based on this table that will display all the months between the StartDate and the EndDate for a given ProjectYearID and when a selection is made, the full date of the last day of the selected
2
12030
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
8976
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 between date and datetime classes? Please, help
4
48210
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 function to return "3/1/2006 8:00". Similarly what function would I use to return the last day of the month? In the above example, the output I would want returned is "3/31/2006 8:00". Thanks, -
0
1896
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 dates only, this code is intended to use more thrifty event descriptions (3rd saturday, last tuesday, etc.) as well as traditional one-of dates. The core here here is quite spartan, no table logic or or db cruft included (I figure you have our own...
3
6114
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
2359
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 ASP.NET a DataGrid with Updatable rows based on a date retrieved from a data table in SQL Server. Below is the design algorithm. 1. Retrieve the Max(rundate) From MyDataTable 2. If the current month -1 = rundate .month from step 1 then 2a. ...
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8591
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
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 we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.