Select to view content in your preferred language

Field calculator, subtract Expiry date from the Current Date

1253
3
Jump to solution
02-12-2013 12:19 AM
AlanGoldsmith
New Contributor
Hi
I am new to Python and not a programmer. I wish to use the field calculator to subtract an "Expiry Date" from the current date. I have used the following code (which works), but it is using a field containing the current date. How do I change this code so it uses only one field and subtracts from the current date?
Many thanks
Alan

Code
import datetime
def NoDays(d1, d2):
  c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")
  c2 = datetime.datetime.strptime (d2.split () [0], "%Y/%m/%d")
  n = (c1 - c2).days
  return n

Expression
NoDays( !Expiry! , !Current!)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Think you can use either the 'today' or 'now' function of datetime.datetime:

On my computer, if I do this at IDLE's command line to test returns of both functions they seem to behave similarly:

>>> testNow = datetime.datetime.now(); testToday = datetime.datetime.today()


This is the result (the same time):

>>> print testNow
2013-02-12 06:20:12.998000

>>> print testToday
2013-02-12 06:20:12.998000


Here's the thing though - you have 2 lines in your def function that converts the 'fed' time values and converts only the date component into a string that includes backslashes '/' ---

c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")
c2 = datetime.datetime.strptime (d2.split () [0], "%Y/%m/%d")

...so 'strptime' needs 2 parameters - '.split()[0]' requires the 'd' values to be string and the 2nd parameter for formatting '%Y/%m/%d' assumes that '/' is in the string, not the '-' that is returned on my system.  So you may have to 'coerce' that to work by changing the explicit formatting, something like this may work, you have to test it:
import datetime def NoDays(d1):      c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")      current = datetime.datetime.today()      c2 = datetime.datetime.strptime (str(current).split () [0], "%Y-%m-%d")      n = (c1 - c2).days      return n



...and your expression would be the date in the past, such as:

NoDays( !Expiry! )


Hope that helps - I cannot be sure of the formatting "%Y-%m-%d", so if it fails try switching it back to "%Y/%m/%d" (which should still work for your field values).

-Wayne

View solution in original post

0 Kudos
3 Replies
MarcinGasior
Occasional Contributor III
Try this code with !Expiry! field only:
def NoDays(d1):
  c1 = datetime.datetime.strptime(d1.split()[0], "%Y-%m-%d").date()
  c2 = datetime.datetime.now().date()
  return (c1-c2).days
0 Kudos
T__WayneWhitley
Frequent Contributor
Think you can use either the 'today' or 'now' function of datetime.datetime:

On my computer, if I do this at IDLE's command line to test returns of both functions they seem to behave similarly:

>>> testNow = datetime.datetime.now(); testToday = datetime.datetime.today()


This is the result (the same time):

>>> print testNow
2013-02-12 06:20:12.998000

>>> print testToday
2013-02-12 06:20:12.998000


Here's the thing though - you have 2 lines in your def function that converts the 'fed' time values and converts only the date component into a string that includes backslashes '/' ---

c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")
c2 = datetime.datetime.strptime (d2.split () [0], "%Y/%m/%d")

...so 'strptime' needs 2 parameters - '.split()[0]' requires the 'd' values to be string and the 2nd parameter for formatting '%Y/%m/%d' assumes that '/' is in the string, not the '-' that is returned on my system.  So you may have to 'coerce' that to work by changing the explicit formatting, something like this may work, you have to test it:
import datetime def NoDays(d1):      c1 = datetime.datetime.strptime (d1.split () [0], "%Y/%m/%d")      current = datetime.datetime.today()      c2 = datetime.datetime.strptime (str(current).split () [0], "%Y-%m-%d")      n = (c1 - c2).days      return n



...and your expression would be the date in the past, such as:

NoDays( !Expiry! )


Hope that helps - I cannot be sure of the formatting "%Y-%m-%d", so if it fails try switching it back to "%Y/%m/%d" (which should still work for your field values).

-Wayne
0 Kudos
AlanGoldsmith
New Contributor
Thank you very much to Marcin and Wayne, it was Wayne's code that solved the issue.
I'll be back!
0 Kudos