Reputation: 11
Is there any python library that would let me convert date time format from '2017-10-25 15:24:38' to '25 Oct 2017 03:24 pm'
in node.js there is a library called moments.js that does it. is there a way to do it in Python?
P.S. I'm using robot framework
Upvotes: 1
Views: 21689
Reputation: 446
You could use:
from datetime import datetime
datetime_object = datetime.strptime('2017-10-25 15:24:38', '%Y-%m-%d %H:%M:%S')
datetime_string = datetime_object.strftime('%d %b %Y %I:%M %p')
EDIT: If you desire AM/PM in lowercase:
dt_s = datetime_object.strftime('%d %b %Y %I:%M')
dt_s_ampm = datetime_object.strftime('%p').lower()
final_dt = dt_s + ' ' + dt_s_ampm
Upvotes: 4
Reputation: 6961
In Robot Framework there is a standard library DateTime that can be used here. The keyword Convert Date
can be used to convert a standard date-time string into another format. The formatting format used by the python command strftime()
Documentation applies here as well.
In the below robot code the formatting is performed:
*** Settings ***
Library DateTime
*** Test Cases ***
TC
${date} Convert Date 2017-10-25 15:24:38 result_format=%d %b %Y %I:%M %p
Which results in the following output:
${date} = 25 Oct 2017 03:24 PM
Upvotes: 1
Reputation: 324
>>> datetime(*strptime(s, "%Y-%m-%dT%H:%M:%S+0000")[0:6]).strftime("%B %d, %Y %I:%M %p")
'July 16, 2013 04:14 PM'
read more about parsing & converting process: https://docs.python.org/2/library/datetime.html
Upvotes: 0