How to return a number as if it were a date?

Asked

Viewed 85 times

0

Receive the date in the dd/mm/yyyy text format and return the date in the yyyymmdd number format (example: take "05/10/1983" and return 19831005). But it presented an error in the syntax.

def data(i):
     dia=i[0:2]
     mes=i[3:5]
     ano=i[6:]
     novadata = int(''".join([ano,mes,dia]))
      return novadata

1 answer

0


Except for the fact that the function is too complex, the only problem is that it has a double quotation mark right there after the two single quotation marks placed as separator for the join().

There’s also a conceptual problem with turning this into an integer, this value is not numerical, it even works, but it doesn’t represent a quantity, at least not in an appropriate way, it can’t account for it because it would have special rules to work, do not use quantitative data for this. It may be that this problem is symptomatic of some major error yet, I do not even know if the date should be treated this way.

def data(i):
    return int(''.join([i[6:], i[3:5], i[0:2]]))
print(data('05/10/1983'))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.