top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Subtracting two dates in python

0 votes
2,479 views

I would like to subtract two dates i.e I have entered a date into a textbox which is of type String like below

type(waitForObject(":VWAP Calculator_LCDateTextField"), "07/24/14")

I am capturing that date like below

Current = (waitForObject(":VWAP Calculator_LCDateTextField").text)

so, now I want to subtract the captured date with my current system date and get the difference in days. I tried many ways and see no success.

Someone please help?

posted Jul 31, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Try the 'datetime' module:

 >>> from datetime import datetime
 >>> d = datetime.strptime("07/24/14", "%m/%d/%y")
 >>> d
datetime.datetime(2014, 7, 24, 0, 0)
 >>> diff = datetime.now() - d
 >>> diff.days
7
answer Jul 31, 2014 by Kumar Mitrasen
0 votes

If you have the user-entered date in a string format, you can use datetime.strptime() to convert it into a datetime object, like so:

 from datetime import datetime
 user_date = datetime.strptime(user_input, "%m/%d/%y")

Get the current system date like this:

 now_date = datetime.now()

Subtract the two datetime objects to obtain a timedelta object:

 mydelta = now_date - user_date

Look at the days attribute to get the difference in days:

 mydelta.days
answer Jul 31, 2014 by Alok Sharma
Similar Questions
0 votes

I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list. I could do:

new_songs = []
old_songs = []
for s in songs:
 if s.is_new():
 new_songs.append(s)
 else:
 old_songs.append(s)

Which works, but is klunky compared to the two-liner above. This seems like a common enough thing that I was expecting to find something in itertools which did this. I'm thinking something along the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?

+1 vote

I'm trying to multiply two matrices that has different size.

import numpy as np

a = np.random.randn(4, 3)
b = np.random.randn(4, 1)

print a
print b

How should I multiply a and b so that the multipled matrix's size is 4*3?

I want to multiply matrix 'b' to each row of matrix 'a'.
So that if matrix a is
[[1, 2, 3],
[2, 3, 4]]
and b is
[[2],
[3]]
a*b is
[[2, 4, 6],
[4, 6 ,8]]

Plz help me, if possible, plz use numpy

+1 vote

I am Automating some repetitive works through Sikuli and Python scripting languages. I have multiple workflows. I need to schedule this script for every two hours. Can anyone guide me how to schedule the scripts for every two hours.

Is there any way to schedule the python programming through Task scheduler in windows platform.

My environment
OS:Windows
Programming languages: Sikuli,Python

...