13

Possible Duplicate:
Python datetime to Unix timestamp

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

Community
  • 1
  • 1
user530476
  • 191
  • 1
  • 1
  • 5

4 Answers4

28

Subtracting two datetime.datetime objects gives you a timedelta object, which has a .total_seconds() method (added in Python 2.7). Divide this by 60 and cast to int() to get minutes since your reference date:

import datetime

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

or in a python shell:

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

For python 2.6 and earlier, you'll have to use the .days and .seconds attributes to calculate the minutes:

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

which gives you an integer as well.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    only 608 minutes from January 1st, doesn't this ring a bell? :) `.seconds` is since midnight, you want `.total_seconds()` – Kos Aug 28 '12 at 09:11
  • 1
    @Kos: My, are we all stumbling today.. All fixed up now (including spelling the `//` operator correctly, really had too little sleep last night). – Martijn Pieters Aug 28 '12 at 09:13
  • Also, the OP needs an `int`. :-) – Kos Aug 28 '12 at 09:18
  • @Kos: yeah, `total_seconds()` is a float, already corrected.. – Martijn Pieters Aug 28 '12 at 09:19
  • : 'datetime.timedelta' object has no attribute 'total_seconds'. The server I am running this on is using Python 2.6.6-possibly too old? – user530476 Aug 28 '12 at 10:06
  • 1
    Too old. :) Martijn has suggested to use `t.seconds // 60 + t.days * 24*60` some edits ago, you can fall back to that if your server is outdated. – Kos Aug 28 '12 at 10:24
  • 1
    @user530476: Yes, I'll update the answer to fit python 2.6 as well (`total_seconds()` was introduced in 2.7). – Martijn Pieters Aug 28 '12 at 12:43
3

By substracting datetimes, you can have a timedelta. This timedelta can be divided itself to give you what you want :

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

(this code is only valid with python3, and that's why everybody should switch to python3 ;-) )

Scharron
  • 17,233
  • 6
  • 44
  • 63
  • Nope, you can't divide a timedelta by another timedelta. TypeError: unsupported operand type(s) for //: 'datetime.timedelta' and 'datetime.timedelta' – Scharron Aug 28 '12 at 09:19
  • Ah, missed something indeed; the SO syntax highlighting incorrectly marked it as a comment and I am not awake enough to notice things.. Nice trick! – Martijn Pieters Aug 28 '12 at 09:21
  • @MartijnPieters Yay, I made the // work ! – Scharron Aug 28 '12 at 09:26
  • Interesting, I wonder which ones of the tags interferes with setting a proper syntax highlighting language here. Normally the `python` tag on the question should be enough to make this work, but if one of the other tags has a *different* language associated with it then the highlighter falls back to auto-detect. – Martijn Pieters Aug 28 '12 at 12:47
  • @MartijnPieters Yep. None of the `language: .*py` worked. The only one was `language-all: lang-py` – Scharron Aug 28 '12 at 12:53
  • I just noticed the same problem on another post; I suspect that the language flag associated with the `python` tag is actually wrong.. – Martijn Pieters Aug 28 '12 at 13:02
  • @MartijnPieters I don't think it's related to tags, I tried with lang-py (so not using tags), and it did not work. It only works with the language-all … – Scharron Aug 28 '12 at 14:09
2

If you want the minutes of the delta between two dates, you can make a datetime.timedelta object by subtracting the two dates (see here), and then retrieve the minutes as shown in this question:

Convert a timedelta to days, hours and minutes

Community
  • 1
  • 1
Lior
  • 2,531
  • 1
  • 15
  • 15
  • Not entirely, the linked question doesn't give you a way to calculate the total minutes in a timedelta. The `.total_seconds()` method wasn't available yet at the time those answers were written, for example. – Martijn Pieters Aug 28 '12 at 09:25
1
>>> import datetime
>>> now = datetime.datetime.now()
>>> then  = datetime.datetime(year=2012, month=1, day=1)
>>> delta=now-then

This is a timedelta object representing an interval of time.

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

To count the minutes during that interval, use:

>>> print delta.total_seconds() / 60
346265.42511666665
Kos
  • 70,399
  • 25
  • 169
  • 233
  • 1
    : 'datetime.timedelta' object has no attribute 'total_seconds' – user530476 Aug 28 '12 at 09:58
  • 1
    Update your Python. [total_seconds](http://docs.python.org/library/datetime.html#datetime.timedelta.total_seconds) was introduced in 2.7 (2010). – Kos Aug 28 '12 at 10:22