473,414 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

how many days each month

Hi
I was wondering how I can get the maximum actual value of the number of days that fall in a month
For example, for April 2008, how many days it is. My python code requires this
Thanks for your help
Oct 6 '09 #1
2 10566
Glenton
391 Expert 256MB
Here's a fairly trivial way (there are probably better ways, but I was just digging through the docshttp://docs.python.org/library/datet...etime.datetime

Expand|Select|Wrap|Line Numbers
  1. from datetime import *
  2. apr=date(2008,4,1)
  3. may=date(2008,5,1)
  4. days_in_apr=(may-apr).days
  5.  
Obviously if you were a little bit slick you'd implement a function which takes 4 or Apr and returns the number of days. There may be such a function, but I got distracted. Hopefully this gets you started...
Oct 6 '09 #2
bvdet
2,851 Expert Mod 2GB
I have played around with a dates class for the exercise. Following is a snippet of the code that may give you some ideas.
Expand|Select|Wrap|Line Numbers
  1. def isLeap(yr):
  2.     if not yr % 4 and (yr % 100 or not yr % 400):
  3.         return [None,31,29,31,30,31,30,31,31,30,31,30,31]
  4.     return [None,31,28,31,30,31,30,31,31,30,31,30,31]
  5.  
  6. monthDict = dict(zip(range(1,13),['January', 'February', 'March',
  7.                                   'April', 'May', 'June', 'July',
  8.                                   'August', 'September', 'October',
  9.                                   'November', 'December']))
  10.  
  11. # Date format MM/DD/YYYY
  12. datestr = "04/22/2001"
  13. month,day,year = [int(item) for item in datestr.split('/')]
  14. days_in_month = isLeap(year)[month]
Oct 6 '09 #3

Sign in to post your reply or Sign up for a free account.

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...
2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
4
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You...
19
by: Ricardo Perez Lopez | last post by:
Hello everyone: I'm a PostgreSQL newbie, working now with dates, times, timestamps and intervals. I have three questions about the above: FIRST: --------
3
by: ri58776 | last post by:
Hi, does anyone have any idea how can we get last date of the month in SQL reportging. I am using following fomula, but it works only for 31 days of month, not for feb,april,june...(30 days) ...
7
by: ajaydesai | last post by:
I have JavaScript code to dispaly two month calendar days at a time, but i have a problem both month that disaplay at a time have same days (for example May and June has same days, June and July have...
7
by: burnside | last post by:
Hi - long time viewer, first time poster i have a car booking system that stores the startdate and the enddate of a booking as well as car/customer details. i want to produce an efficiency report...
4
by: tbramley | last post by:
Hi, I've got a cross tab query which is sorted by area then month but the month is sorted in alphabetical order and I was wondering if this can be done in Month order e.g. April, May, June, July...
15
karthickkuchanur
by: karthickkuchanur | last post by:
Hai sir, i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically, i refer to google it was confusing...
5
by: Lado.Leskovec | last post by:
Hi! I'm writing a script that would sort some events and calculate the sum of them all in the end of the month, which should be equal to the number of days in a month. It seems to be working...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.