473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

datetime: the date of the day one month ago...how?

hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006

so:

one-month-ago(12.apr.2006 ) = 12.mar.2006

of course sometimes it gets more complicated, like:

one-month-ago(31.mar.2006 )

or

one-month-ago(1.jan.2006)

the datetime.timede lta objects only work with hours or days or weeks,
not month (i understand why)...

but is there a way to calculate this in python?

i really don't want to calculate it by myself :-))

thanks,
gabor
Apr 12 '06 #1
4 10112
gabor wrote:
hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006

so:

one-month-ago(12.apr.2006 ) = 12.mar.2006

of course sometimes it gets more complicated, like:

one-month-ago(31.mar.2006 )

or

one-month-ago(1.jan.2006)

the datetime.timede lta objects only work with hours or days or weeks,
not month (i understand why)...

but is there a way to calculate this in python?

i really don't want to calculate it by myself :-))

It is application specific. So how *do* you want
one-month-ago(31.mar.2006 ) or one-month-ago(28.feb.2006 ) to work? No one
can know but you.
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
Apr 12 '06 #2
gabor wrote:
hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006


dateutil has one implementation of this:
http://labix.org/python-dateutil

Kent
Apr 12 '06 #3
Max M wrote:
gabor wrote:
hi,

i'm trying to get the date of the day one month ago.

for example:

today = 12.apr.2006
one-month-ago = 12.mar.2006

so:

one-month-ago(12.apr.2006 ) = 12.mar.2006

of course sometimes it gets more complicated, like:

one-month-ago(31.mar.2006 )

or

one-month-ago(1.jan.2006)

the datetime.timede lta objects only work with hours or days or weeks,
not month (i understand why)...

but is there a way to calculate this in python?

i really don't want to calculate it by myself :-))

It is application specific. So how *do* you want
one-month-ago(31.mar.2006 ) or one-month-ago(28.feb.2006 ) to work? No one
can know but you.


well, give me a solution for ANY behaviour :)

or, let's specify it then:

i want the day that you get by intutively saying "one month ago". means
usually picking the same day in the previous month. if that day does not
exist, i want the nearest day that exist and was BEFORE the nonexistent day.

one-month-ago(31.mar.2006 ) = 28.feb.2006
one-month-ago(28.feb.2006 ) = 28.jan.2006
so, now that we have a spec, is there a way to achieve this in python
without writing the algorithm by myself?
thanks,
gabor
Apr 12 '06 #4
On Wed, 12 Apr 2006 13:42:15 +0200, rumours say that gabor
<ga***@nekomanc er.net> might have written:
i want the day that you get by intutively saying "one month ago". means
usually picking the same day in the previous month. if that day does not
exist, i want the nearest day that exist and was BEFORE the nonexistent day.

one-month-ago(31.mar.2006 ) = 28.feb.2006
one-month-ago(28.feb.2006 ) = 28.jan.2006


def submonth(d):
year, month= d.year, d.month
if month == 1:
year-= 1; month= 12
else:
month-= 1
try:
return d.replace(year= year, month=month)
except ValueError:
return d.replace(day=1 ) - datetime.timede lta(1)

submonth(dateti me.date(2006,3, 31)) datetime.date(2 006, 2, 28) submonth(dateti me.date(2006,2, 28))

datetime.date(2 006, 1, 28)

--
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians
Apr 12 '06 #5

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

Similar topics

14
6443
by: Paul Moore | last post by:
I was just writing some code which did date/time manipulations, and I found that the Python 2.3 datetime module does not supply a number of fairly basic functions. I understand the reasoning (the datetime module provides a set of datatypes, and doesn't attempt to get into the murky waters of date algorithms) but as these things can be quite tricky to get right, I was wondering if anyone has already implemented any date algorithms, or...
6
1681
by: Terry Hancock | last post by:
What do you do when a date or time is incompletely specified? ISTM, that as it is, there is no formal way to store this --- you have to guess, and there's no way to indicate that the guess is different from solid information. As a result, I have sometimes had to abandon datetime, even though it seemed like the logical choice for representing data. E.g. I might have information like "this paper was published in May 1997". There's no...
3
9735
by: sunny076 | last post by:
Hi, I am trying to convert from Julian to Gregorian data in C#. I was exploring teh JulianCalendar and Gregorian calendar classes but still not sure how I can do it. For example, the Julian date is 100033 and I know that it is 2/2/2000 in Gregorian. I would appreciate if anyone can help point me out.
5
3951
by: Abdolhosein Vakilzadeh Ebrahimi | last post by:
Is there any way to construct a System.DateTime which doesn't check year, month and day to be in correct range== no validation? Thanks A.V.Ebrahimi
6
8994
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
2
1379
by: Tim::.. | last post by:
Hi I'm trying to create a pager that allows you to show records by year and month! I am getting close but would really appritiate some help! Can someone please tell me how I convert the value of (i) into a date so that l.Text shows the date as Feb or Mar etc etc EG: If i = 1
24
2041
by: rn5a | last post by:
The date in my local machine is set to the dd/MM/yyyy format. When I insert a date in a MS-Access DB table, it gets populated in the above format. For e.g. if the date is, say, 8th March 2007, it gets populated in the DB table as 08/03/2007 In other words, first the day is shown, then the month & finally the year but when I retrieve it in ASP.NET & using DatePart, try to extract the day & the month like this (assume that the date...
0
16514
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any formatting considerations at all. They are simple, easy, and brief and you should use them any time you need to incorporate any date literals or date math in your T-SQL code. create function DateOnly(@DateTime DateTime) -- Returns @DateTime...
0
9722
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
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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...
1
10391
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
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...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.