Convert date into format %d%m%y Python [duplicate] - python

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'

Related

python convert string to date time [duplicate]

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.

Numeric date changed to String [duplicate]

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

How to calculate UNIX timestamp difference using python? [duplicate]

This question already has answers here:
How to format date string via multiple formats in python
(5 answers)
python time string does not match format
(1 answer)
Closed 4 years ago.
I want the diff between two timestamps which are available to me as list of strings.
date_list = ['Tue Sep 25 21:12:32 PDT 2018', 'Tue Sep 25 21:15:27 PDT 2018']
How do I get the difference in minutes or in hours ?
I saw some examples, but they are using different format of UNIX timestamp. I can't change the given date format.
I suggest to use dateutil.
For instance
from dateutil import parser
d1 = parser.parse(date_list[0])
d2 = parser.parse(date_list[1])
at this point d1 and d2 are standard datetime objects and you can use the standard methods/operators to operate on them

Compare unicode and datetime.datetime in python [duplicate]

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 8 years ago.
I have two dates:
Sat Mar 15 19:47:17 +0000 2014
2014-03-12 19:50:22.159411+00:00
I need to compare these two dates but I get the error
TypeError: can't compare datetime.datetime to unicode
How should I convert one of them?
The easiest method is to use the 3rd party dateutil lib, and do:
from dateutil.parser import parse as parse_date
unicode_text = 'Sat Mar 15 19:47:17 +0000 2014'
dt = parse_date(unicode_text)
# 2014-03-15 19:47:17+00:00
if dt == other_datetime:
# do something

How to convert a date string to different format [duplicate]

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.

Categories

Resources