How can I get the number of seconds since, say, 00:00:00 on 1st January 2016 in Python?
Asked
Active
Viewed 1,439 times
-12
-
1This can be solved with a simple Google search. – Rob Murray Jan 12 '16 at 13:21
-
1What's up with these questions of late? Of the _zero effort, please present me with the code_-type. – Nelewout Jan 12 '16 at 13:23
-
2@N.Wouda A topic for SO Meta if not already asked? – Rob Murray Jan 12 '16 at 13:24
-
1@R.Murray I am quite sure it would be a dupe haha :) – Nelewout Jan 12 '16 at 13:24
-
@N.Wouda Most probably xD – Rob Murray Jan 12 '16 at 13:25
-
@N.Wouda No need to be so rude, sorry. – j x Jan 12 '16 at 13:26
-
@JS I am not being rude. Your question does not show any research effort, yet you do ask us to pretty much present you with a solution. Stack Overflow is not a code writing service, and I suggest you read the [how to ask](http://stackoverflow.com/help/how-to-ask) first. – Nelewout Jan 12 '16 at 13:28
-
1@N.Woulda simply refer me to the link to the duplicate thread next time and say nothing more. – j x Jan 12 '16 at 13:42
-
@JS there'd be no need for that if some research effort from your side was conducted first. You've posted such nice questions before, don't slack now! :) – Nelewout Jan 12 '16 at 13:49
-
@R.Murray: no need to be snob. At least, make sure you are (technically) right if you do so. [See this answer, to understand that it is not **that** simple to find the number of seconds since a particular time (0:00:00 on 1st January 2016) even if we ignore leap seconds](http://stackoverflow.com/a/26313848/4279). The question that you've linked doesn't take dates into account and (naturally) it doesn't mention timezones that may affect the answer in general. – jfs Jan 13 '16 at 06:32
-
@N.Wouda: could you be kind enough to point me to a duplicate? (I've searched and I haven't found one -- and no: the question linked by R. Murray is not it) – jfs Jan 13 '16 at 06:37
-
here's a [question that shows how you could work with leap seconds in Python](http://stackoverflow.com/q/33415475/4279). Note: [legally elapsed seconds may be different in different countries](http://www.ucolick.org/~sla/leapsecs/epochtime.html). Clarify your question: whether you care about timezones, leap seconds, legal stuff. – jfs Jan 13 '16 at 06:54
-
1@J.F.Sebastian this did not afflict OP. No leap seconds occured since his specified date. Anyways, _if_ this is a problem for him (it'd be specific enough to maybe warrant an answer, save for duplicates), I'd definitely help him look for a solution - but a single liner is not going to cover such details. A single line is not going to cover much in general. – Nelewout Jan 13 '16 at 07:27
-
1@J.F.Sebastian It was not clear from the question that leap seconds should be taken in to account and I am not sure where you got this inference from. If the question was more specific in asking about this I would agree with you, but I don't think this one sentence question implies this at all. If you are interested in this question then it sounds like a perfectly valid question to ask. – Rob Murray Jan 13 '16 at 07:29
-
@R.Murray: do see *"even if we ignore leap seconds"* in my comment? Click the link in the comment and read the answer. – jfs Jan 13 '16 at 07:32
-
@N.Wouda: it is not about leap seconds, [read this answer](http://stackoverflow.com/a/26313848/4279) – jfs Jan 13 '16 at 07:33
-
1@J.F.Sebastian don't get me wrong, I agree it's hard and I agree that we should take all these things into account, _if OP is pointing this out specifically_. But we are just as much making assumptions now on what problems OP could be running into. Re-read his question: there's _nothing_ there to point us to the finer details of his problem. – Nelewout Jan 13 '16 at 07:36
-
@N.Wouda: I'm still waiting for a link to the duplicate from you. You may say the question is not specific but if OP were aware about these issues; OP wouldn't have asked the question in the first place. But you didn't ask OP to clarify, to make the question more specific as I did. – jfs Jan 13 '16 at 07:42
-
@J.F.Sebastian Your answer to that question is thorough and I do see now that your solution actually is better in this case as it explains how to find if multiple days have passed. However the OP did not make it clear that there was a specific problem with finding multiple days in seconds due to the one-line style of the question. – Rob Murray Jan 13 '16 at 07:42
-
@R.Murray: if you think the question is not specific enough; why didn't you asked OP to clarify as I did? – jfs Jan 13 '16 at 07:43
-
1@J.F.Sebastian because the question may not be worth it. Also, if the link R. Murray presented does not cover the problem, why did OP not get back to us? – Nelewout Jan 13 '16 at 07:45
-
1@J.F.Sebastian On first inspection the question seemed like a question in the style of "how do I do... ?". Also mirroring what N. Wouda said, why didn't the OP get back to us if he was not satisfied? – Rob Murray Jan 13 '16 at 07:46
-
@R.Murray: if you two don't know that timezones may affect the result; why do you think OP should know that? – jfs Jan 13 '16 at 07:48
2 Answers
1
To get that time difference in your timezone in seconds:
import time
import datetime
today = datetime.datetime.today()
start_date = datetime.datetime(2016, 1, 1, 0, 0, 0)
time_difference = abs(today - start_date)
print time_difference.seconds
Please read the date time documentation at: https://docs.python.org/2/library/datetime.html#module-datetime
If you want a negative time difference for a date in the future remove the abs()
function.

vrleboss
- 463
- 4
- 24