Questions tagged [python-datetime]

Python's built-in datetime module provides classes for manipulating dates and times in both simple and complex ways.

Python's built-in datetime module provides classes for manipulating dates and times in both simple and complex ways.

View on Python docs - for Python 2 and Python 3.

1880 questions
908
votes
12 answers

Getting today's date in YYYY-MM-DD in Python?

Is there a nicer way than the following to return today's date in the YYYY-MM-DD format? str(datetime.datetime.today()).split()[0]
Pyderman
  • 14,809
  • 13
  • 61
  • 106
795
votes
16 answers

How to make a datetime object aware (not naive)

What I need to do I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for this…
Mark Tozzi
  • 10,353
  • 5
  • 22
  • 30
467
votes
6 answers

Convert DataFrame column type from string to datetime

How can I convert a DataFrame column of strings (in dd/mm/yyyy format) to datetime dtype?
perigee
  • 9,438
  • 11
  • 31
  • 35
326
votes
14 answers

How to convert a UTC datetime to a local datetime using only standard library?

I have a python datetime instance that was created using datetime.utcnow() and persisted in database. For display, I would like to convert the datetime instance retrieved from the database to local datetime using the default local timezone (i.e., as…
Nitro Zark
  • 3,377
  • 2
  • 15
  • 6
240
votes
15 answers

How to calculate the time interval between two time strings

I have two times, a start and a stop time, in the format of 10:33:26 (HH:MM:SS). I need the difference between the two times. I've been looking through documentation for Python and searching online and I would imagine it would have something to do…
Hannah
  • 2,805
  • 4
  • 19
  • 10
161
votes
4 answers

Calculate Time Difference Between Two Pandas Columns in Hours and Minutes

I have two columns, fromdate and todate, in a dataframe. import pandas as pd data = {'todate': [pd.Timestamp('2014-01-24 13:03:12.050000'), pd.Timestamp('2014-01-27 11:57:18.240000'), pd.Timestamp('2014-01-23 10:07:47.660000')], 'fromdate':…
sbalajis
  • 1,939
  • 3
  • 14
  • 14
102
votes
4 answers

Convert string into datetime.time object

Given the string in this format "HH:MM", for example "03:55", that represents 3 hours and 55 minutes. I want to convert it to datetime.time object for easier manipulation. What would be the easiest way to do that?
Zed
  • 5,683
  • 11
  • 49
  • 81
80
votes
13 answers

How to convert a time string to seconds?

I need to convert time value strings given in the following format to seconds, for example: 1.'00:00:00,000' -> 0 seconds 2.'00:00:10,000' -> 10 seconds 3.'00:01:04,000' -> 64 seconds 4.'01:01:09,000' -> 3669 seconds Do I need to use regex to do…
damon
  • 8,127
  • 17
  • 69
  • 114
72
votes
9 answers

How do I determine if current time is within a specified range using Python's datetime module?

What would be the best way to see if the current time lies between say 10:30 AM and 4:30 PM. I could think of the following, not sure how correct: from datetime import datetime nw = datetime.now() hrs = nw.hour;mins = nw.minute;secs =…
user993563
  • 18,601
  • 10
  • 42
  • 55
65
votes
5 answers

How do I round datetime column to nearest quarter hour

I have loaded a data file into a Python pandas dataframe. I has a datetime column of the format 2015-07-18 13:53:33.280. What I need to do is create a new column that rounds this out to its nearest quarter hour. So, the date above will be rounded…
sfactor
  • 12,592
  • 32
  • 102
  • 152
59
votes
3 answers

Why is pandas.to_datetime slow for non standard time format such as '2014/12/31'

I have a .csv file in such format timestmp, p 2014/12/31 00:31:01:9200, 0.7 2014/12/31 00:31:12:1700, 1.9 ... and when read via pd.read_csv and convert the time str to datetime using pd.to_datetime, the performance drops dramatically. Here is a…
liubenyuan
  • 733
  • 1
  • 5
  • 10
51
votes
4 answers

Python datetime strptime() and strftime(): how to preserve the timezone information

See the following code: import datetime import pytz fmt = '%Y-%m-%d %H:%M:%S %Z' d = datetime.datetime.now(pytz.timezone("America/New_York")) d_string = d.strftime(fmt) d2 = datetime.datetime.strptime(d_string, fmt) print d_string print…
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
50
votes
6 answers

Is there an easy way to convert ISO 8601 duration to timedelta?

How can I convert an ISO 8601 duration string to datetime.timedelta? I tried just instantiating timedelta with the duration string and a format string, but I get an exception: >>> from datetime import timedelta >>> timedelta("PT1H5M26S",…
Alkindus
  • 2,064
  • 2
  • 16
  • 16
42
votes
5 answers

How to convert integer into date object python?

I am creating a module in python, in which I am receiving the date in integer format like 20120213, which signifies the 13th of Feb, 2012. Now, I want to convert this integer formatted date into a python date object. Also, if there is any means by…
Uday0119
  • 770
  • 1
  • 7
  • 23
36
votes
2 answers

How can I translate dates and times from natural language to datetime?

I'm looking for a way to translate 'tomorrow at 6am' or 'next monday at noon' to the appropriate datetime objects. I thought of engineering a complex set of rules, but is there another way?
Vasil
  • 36,468
  • 26
  • 90
  • 114
1
2 3
99 100