How to convert Dates into Specified Format using python? - python

I have a list of dates as below:
list_dates = ['2013-05-01', '15th Oct 2013', '01-05-13', '2013/FEB/05',. . .]
What I want is:
list_dates = ['2013-05-01', '2013-10-15', '2013-05-01', '2013-02-05', . . .]
Is there ANY way, or package/module to accomplish this task?

You can try using the third-party library dateutil's parser, which is usually very good at determining the proper way to parse a date (at least way better than I'll ever be :) ). The result of parser.parse is a datetime object, on which you can call strftime with your desired format:
In [1]: from dateutil import parser
In [2]: list_dates = ['2013-05-01', '15th Oct 2013', '01-05-13', '2013/FEB/05']
In [3]: [parser.parse(date).strftime('%Y-%m-%d') for date in list_dates]
Out[3]: ['2013-05-01', '2013-10-15', '2013-01-05', '2013-02-05']

You can use datetime and time modules for this.
http://docs.python.org/2/library/datetime.html
http://docs.python.org/2/library/time.html

Try datetime.datetime, it provide strptime and strftime method to do this task.

Related

Python: How to display date time in following format (2018-06-25T07:17:17.000Z)?

I want to display the datetime in the following format using python:
2018-06-25T07:17:17.000Z
I am trying to convert using strftime:
datetime.datetime.now().strftime("%Y-%m-%dTH:M:SZ")
but it seems that doesn't work.
What i am missing?
you can use
import datetime
datetime.datetime.today().isoformat()
2018-06-25T07:17:17.000Z
This format is called ISO format, after standard ISO 8601. The datetime object has a isoformat method to output this form.
strftime("%Y-%m-%dTH:M:SZ")
You seem to have forgotten some % before the H, M, and S. Try strftime("%Y-%m-%dT%H:%M:%SZ").
but it seems that doesn't work.
Generally it works better if you specify exactly what doesn't work, or what you expect and how the reality differs from your expectation.
You can use following formating for date conversion.
>>> import datetime
>>> today_date = datetime.datetime.now()
>>> today_date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
'2018-06-25T15:50:18.313620Z'
Please let me know,if this is the one you needed.

python convert human to timestamp

My date is in following format:
19/Jun/2014:00:03:09
How to I convert it to epoch timestamp in python?
Note: I searched on date format in python, but could not find any format that matches above.
Thanks.
Use strptime and then mktime.
import time
tt = time.strptime("19/Jun/2014:00:03:09","%d/%b/%Y:%H:%M:%S")
print time.mktime(tt)
import datetime
datetime.datetime.strptime(s, '%d/%b/%Y:%H:%M:%S')
reference
use dateutil it will parse just about anything
$ easy_install python-dateutil
>>> import dateutil.parser as parser
>>> some_date_string = "19/Jun/2014:00:03:09"
>>> parser.parse(some_date_string)
[edit] oops nevermind ... apparently it cant parse this ...

Convert date formats to another with Python

I download RSS content from different countries with Python, but each of them use their own datetime format or time zone. For instance,
Wed, 23 Oct 2013 17:44:13 GMT
23 Oct 2013 18:21:04 +0100
23 Oct 2013 13:12:41 EDT
10-23-2013 00:12:24
At the moment, my solution is to create a different function for each RSS source and change the date to a format I will decide. But is there any way to do this automatically?
Not really. But take a look at the feedparser lib.
Different feed types and versions use wildly different date formats.
Universal Feed Parser will attempt to auto-detect the date format used
in any date element, and parse it into a standard Python 9-tuple, as
documented in the Python time module.
From the list of Recognized Date Formats it seems to me, that the library could help you out some of the way :)
Best of luck
You can try using the dateutil module to parse the datetime.
It povides the functionality to parse most of the known datetime format. Here is an example from the docs:
>>> from dateutil.parser import *
>>> parse("Thu Sep 25 10:36:28 2003")
datetime.datetime(2003, 9, 25, 10, 36, 28)
It returns a datetime object which can be directly used for manipulation. You can then also use strftime to convert it to the required format string.

Python module for date manipulation

Is there a python module specifically for date manipulation. Something equivalent to the lubridate package in R.
Python have a built-in module for handling datetime..You can try that..!!
But if want something extended (like want to build a generic datetime parser), go for python-dateutil
from dateutil.parser import parse
datetimeObj = parse(strDate)
# str date is a date string
Actually, Python's standard datetime module is pretty basic. If you want more extensive and flexible date handling, you can try dateutil or mxDateTime.
The python module arrow seems to be an equivalent of the R lubridate package:
import arrow
In [9]: arrow.get('Julie was born in May 1990', 'MMM YYYY')
Out[9]: <Arrow [1990-05-01T00:00:00+00:00]>
Try to use pytz
Documentation
http://pytz.sourceforge.net/

Python DateUtil Converting string to a date and time

I'm trying to convert a parameter of type string to a date time. I'm using the dateUtil library
from dateutil import parser
myDate_string="2001/9/1 12:00:03"
dt = parser.parse(myDate_string,dayfirst=True)
print dt
every time i run this i get
2001-09-01 12:00:03
regardless of whether i have dayfirst set as true or Year first set as false. Ideally I just want to have a date in the format DD-MM-YYYY HH:MM:SS. I don't want anything fancy. I am willing to use the datetime library but this doesn't seem to work at all for me. Can anyone give simple expamples of how to convert strings to date time with an explicit format, I'm a noob, so the most basic examples are all i require. I'm using Python 2.7
The problem you're having is that any arguments you pass to parser.parse only affect how the string is parsed, not how the subsequent object is printed.
parser.parse returns a datetime object - when you print it it will just use datetime's default __str__ method. If you replace your last line with
print dt.strftime("%d-%m-%Y %H:%M:%S")
it will work as you expect.
The standard lib (built-in) datetime lib can do this easily.
from datetime import datetime
my_date_string = "2001/9/1 12:00:03"
d = datetime.strptime(my_date_string, "%Y/%m/%d %H:%M:%S")
print d.strftime("%d-%m-%Y %H:%M:%S")

Categories

Resources