473,387 Members | 1,621 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,387 software developers and data experts.

How to get a date after a month in python

Suppose i have a date as (2007 / 04 / 25)


now i need a date exactly after a month i.e. (2007 / 05 / 25)

Is there any function which can help it out
Apr 25 '07 #1
4 1638
bvdet
2,851 Expert Mod 2GB
Suppose i have a date as (2007 / 04 / 25)


now i need a date exactly after a month i.e. (2007 / 05 / 25)

Is there any function which can help it out
Expand|Select|Wrap|Line Numbers
  1. >>> import datetime
  2. >>> dt = '2007 / 04 / 25'
  3. >>> d = datetime.date(*[int(i) for i in dt.split(' / ')])
  4. >>> d1 = d.replace(month=d.month+1)
  5. >>> dt = d1.strftime('%Y / %m / %d')
  6. >>> dt
  7. '2007 / 05 / 25'
  8. >>> 
Apr 25 '07 #2
dshimer
136 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. (*[int(i) for i in dt.split(' / ')]) 
I have see you use this syntax before, could you (or anyone who gets it explain it once, or tell me where I can get a decent explanation.

I am refering to the idea of putting the for loop and assigning the return values all in a single statement.

I see it work, but just don't get the mechanics of it.
Apr 25 '07 #3
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. (*[int(i) for i in dt.split(' / ')]) 
I have see you use this syntax before, could you (or anyone who gets it explain it once, or tell me where I can get a decent explanation.

I am refering to the idea of putting the for loop and assigning the return values all in a single statement.

I see it work, but just don't get the mechanics of it.
It wasn't easy for me to understand either. There are two components to explain. First is the list comprehension of the form '[expression for item in list if condition]'.
Expand|Select|Wrap|Line Numbers
  1. >>> [i for i in range(10)]
  2. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. >>> [(i+12)/4.5 for i in range(10) if i == 6]
  4. [4.0]
  5. >>> 
The '*' preceding the list comprehension expands the list as though the list elements were passed to the function as arguments. I think of it as the '*' removes the list brackets. In the example, the equivalent code is:
Expand|Select|Wrap|Line Numbers
  1. dt = '2007 / 05 / 25'
  2. dtList = dt.split(' / ')
  3. d = datetime.date(int(dtList[0]), int(dtList[1]), int(dtList[2]))
  4.  
  5. print d
  6. print repr(d)
  7. '''2007-05-25
  8. datetime.date(2007, 5, 25)
  9. >>>
  10. '''
HTH :)
Apr 25 '07 #4
dshimer
136 Expert 100+
WOW! Thanks, that is too cool.
Apr 25 '07 #5

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

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
1
by: Graeme Longman | last post by:
Hi everyone, I was wondering if anyone has written some Python code which uses a start date and end date and a given interval (day, month or year) and outputs all the time periods for that range...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
4
by: gabor | last post by:
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:
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
2
by: Niyazi | last post by:
Hi everyone, I have a sql table that has 5 column as: cl1Month - cl1_3Month - cl3_6Month - cl6_12Month - clMoreThan12Month Now I have to date and I have to find the differences and check as...
3
by: Bob Sanderson | last post by:
I have a PHP web page which uses a HTML form. I would like to enter dates into the date fields using a JavaScript calendar, similar to the way phpMyAdmin does. Can anyone recommend a JavaScript...
10
by: kyosohma | last post by:
Hi, I am working on a timesheet application in which I need to to find the first pay period in a month that is entirely contained in that month to calculate vacation time. Below are some example...
2
by: mshroom12 | last post by:
I am having trouble with the following project on hand. I use Eclipse to do my work in Java. This is what I'm supposed to complete. Date Validation In this exercise you will write a program...
16
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.