Convert date and time from a specified date to minutes

Possible duplicate:
Python date and time for Unix timestamp

Is there a way to convert datetimeto int, representing minutes, since, for example, January 2012, so that this intcan be changed, written to the database, compared and so on? EDIT: The server I am running this uses Python 2.6.6

+5
source share
4 answers

Subtracting two objects datetime.datetime, you get timedeltaan object that has (added in Python 2.7). Divide this by 60 and type to get minutes from your original date: .total_seconds()int()

import datetime

january1st = datetime.datetime(2012, 01, 01)
timesince = datetime.datetime.now() - january1st
minutessince = int(timesince.total_seconds() / 60)

or in python shell:

>>> import datetime
>>> january1st = datetime.datetime(2012, 01, 01)
>>> timesince = datetime.datetime.now() - january1st
>>> minutessince = int(timesince.total_seconds() / 60)
>>> minutessince
346208

python 2.6 .days .seconds :

minutessince = timesince.days * 1440 + timesince.seconds // 60

.

+14

, datetime.timedelta, (. ), , :

timedelta ,

+2

datetimes, timedelta. timedelta , , :

(datetime.datetime.now() - datetime.datetime(2012, 1, 1)) // datetime.timedelta(minutes=1)

( python3, python3;-))

+1
>>> import datetime
>>> now = datetime.datetime.now()
>>> then  = datetime.datetime(year=2012, month=1, day=1)
>>> delta=now-then

timedelta, .

>>> print delta
240 days, 11:05:25.507000

, :

>>> print delta.total_seconds() / 60
346265.42511666665
+1

All Articles