This question already has answers here:
Parse date string and change format
(10 answers)
How to convert a date string to different format [duplicate]
(2 answers)
Closed 3 years ago.
I need help creating a function that takes a user input(Numeric Date) and turns it into a string
For Example:
input: 2018-06-20
output: June 20th 2018
any hints or code would help me out. Thank you
import datetime
d = '2018-06-20'
datetime.datetime.strptime(d, '%Y-%m-%d').strftime('%B %d %Y')
Output:
June 20 2018
For more formatting you can look here
Related
This question already has answers here:
datetime from string in Python, best-guessing string format
(4 answers)
How can I parse multiple (unknown) date formats in python?
(4 answers)
How can I translate dates and times from natural language to datetime? [closed]
(2 answers)
How to format date string via multiple formats in python
(5 answers)
Closed 2 years ago.
I have a problem in Python 3.9 64x bit.
In a program I am writing, I need to be able to convert any inputted date into the format %d%m%y.
For example, if the user entered 12 December 2021, the program will convert it to 121221, and if the user enters 2021 12 December, it will still convert it to 121221.
You could use pandas to_datetime and then strftime.
from pandas import to_datetime
to_datetime('12 December 2021').strftime('%d%m%y') ## returns 121221
to_datetime('2021 12 December').strftime('%d%m%y') ## returns 121221
pandas tries to infer the format when parsing the string.
Note, without specifying the datetime format for the string entered by the user there is of course ambiguity. E.g. what is meant by a the string '11/12/2021'. It could be '11 December 2021' or '12 November 2021'.
this of course is error prone if the user enters
>>> from_date="12 December 2021"
>>> import time
>>> conv=time.strptime(from_date,"%d %B %Y")
>>> time.strftime("%d/%m/%y",conv)
'121221'
>>> from_date="2021 12 December"
>>> import time
>>> conv=time.strptime(from_date,"%Y %d %B")
>>> time.strftime("%d%m%y",conv)
'121221'
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parse date string and change format
(10 answers)
Closed 2 years ago.
I have a string like this: Monday April 27 2020, 5:14pm
And I want to convert it to date time in python to become: 24-Apr-2020 5:14pm
Would python be able to do that?
from datetime import datetime
str_time = "Monday April 27 2020, 5:14pm"
formatted_time = datetime.strptime(a,"%A %B %d %Y, %I:%M%p")
new_str_representation = formatted_time.strftime("%d-%a-%Y %I:%M%p")
Converting string into datetime
https://docs.python.org/3/library/datetime.html
You can find more informations here.
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parsing datetime in Python..?
(2 answers)
Closed 4 years ago.
I'm receiving data from the port.
This data has date and time.But not formatted.
Example:
'02012019', '090253'
I want to save it to the database and I want to convert this format.
02-01-2019 09:02:53
Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.
Anybody have a suggestion?
from datetime import datetime
date_data ='02012019 09:02:53'
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')
# output '02-01-2019 09:02:53'
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 4 years ago.
This question is very similar to this SO, but my data is on the form:
'13-04-2018 14:06:26.866'
'13-04-2018 14:06:27.131'
'13-04-2018 14:06:27.404'
'13-04-2018 14:06:27.674'
...
i.e. the seconds are given as decimals. My reading of the datetime documentation suggests that it doesn't support this format, so I am not sure how to best proceed.
Looks like you need
.%f == Microsecond as a decimal number, zero-padded on the left.
Ex:
import datetime
s = '13-04-2018 14:06:26.866'
print(datetime.datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f"))
Output:
2018-04-13 14:06:26.866000
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 7 years ago.
I need to convert date string "2013-1-25" to string "1/25/13" in python.
I looked at the datetime.strptime but still can't find a way for this.
I assume I have import datetime before running each of the lines of code below
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13".
If you can't live with the leading zero, try this:
dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)
This prints "1/25/13".
EDIT: This may not work on every platform:
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
If you can live with 01 for January instead of 1, then try...
d = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print datetime.date.strftime(d, "%m/%d/%y")
You can check the docs for other formatting directives.