1

so I'm trying to figure out a way to convert a normal date in the format "dd/mm/yyyy" (for example : 31/12/2016).

I want to find a way to convert this date into a unique number, so I can re-convert it back.

for example i thought of sum=day+month*12 + year*365 as the number and then :

(sum % 365 ) / 12...

but it's not working for each statment. so any ideas?

dan
  • 105
  • 1
  • 13
  • Store year, day and month as string, concat it, convert it to int and when you want to reconvert it just go the same way backwards. – Lehue Nov 10 '16 at 13:49

2 Answers2

0

It is far better not to handle the strings yourself. Instead use the module called datetime. Here is an incredibly good answer which should hopefully satisfy what you need to do. Converting string into datetime

For example, in your case you would need the following

import datetime
your_date = datetime.datetime.strptime("31/12/2016", "%d/%m/%Y")

Then this article How to convert datetime to integer in python explains how you can turn it into an integer, but as stated in the answer, this is usually a bad idea.

Community
  • 1
  • 1
A. N. Other
  • 392
  • 4
  • 14
0

You can use datetime module to extract the day, month, and year and then use as you want.

In [9]: (y, m, d) = str(datetime.date.today()).split('-')[:3]

In [10]: y, m, d
Out[10]: ('2016', '11', '10')

The output is in string format, which can be converted to integers.

In [11]: int(y), int(m), int(d)
Out[11]: (2016, 11, 10)
SunilThorat
  • 1,672
  • 2
  • 13
  • 15