How does python calculate the difference in seconds between the time for today and the date of birth that the user enters himself?
- 6Where are the difficulties? Show the code. - 0xdb
- oneHint: use the datetime module - gil9red
- Write at least in what format the transmitted data will be, the date of birth can be betrayed in different ways. - Igor Igoryanych
- Related question: How to add 30 minutes to the current date? - jfs
|
2 answers
Suppose you were born in New York, then the time of birth is recorded to the nearest minute. Since you can now be in a different time zone, you should explicitly specify the time zone:
>>> import pendulum # $ pip install pendulum >>> birthtime = pendulum.create(1999, 9, 11, 23, 10, tz='America/New_York') >>> age = pendulum.now() - birthtime >>> age.in_seconds() 569173149 - And if born in Vasyuki, then the time of birth is not recorded to the minute? ;-) - Kromster
- @Kromster: on the birth certificates I only see the day. - jfs
- Why put a minus answer? Answer the mean cowardly;) - jfs
- So in the question only the date appears. In general, the curtsy towards watches / belts / minutes is not very clear. not minus - Kromster
- @Kromster: the answer to the question "how to calculate the difference in seconds" must take into account time zones. If it is not clear, then you can read more on the link, I quoted above - jfs
|
Import datetime, it has such a function as timedelta, which calculates the difference.
- onenaive subtraction without taking into account the time zone, can make a avoidable error in the calculations (for the birthday it may not matter, but in the general case it is advisable not to introduce hourly errors when subtracting time). - jfs
|