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
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
- >>> import datetime
-
>>> dt = '2007 / 04 / 25'
-
>>> d = datetime.date(*[int(i) for i in dt.split(' / ')])
-
>>> d1 = d.replace(month=d.month+1)
-
>>> dt = d1.strftime('%Y / %m / %d')
-
>>> dt
-
'2007 / 05 / 25'
-
>>>
- (*[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.
bvdet 2,851
Expert Mod 2GB - (*[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]'. - >>> [i for i in range(10)]
-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-
>>> [(i+12)/4.5 for i in range(10) if i == 6]
-
[4.0]
-
>>>
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: - dt = '2007 / 05 / 25'
-
dtList = dt.split(' / ')
-
d = datetime.date(int(dtList[0]), int(dtList[1]), int(dtList[2]))
-
-
print d
-
print repr(d)
-
'''2007-05-25
-
datetime.date(2007, 5, 25)
-
>>>
-
'''
HTH :)
WOW! Thanks, that is too cool.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 $...
|
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...
|
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...
|
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:
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
| |