strptime

how to convert a string containing characters into date time using strptime in Python

how to convert a string containing characters into date time using strptime in Python Question: I’m trying to convert a string where there exists a characters between date information into a datetime. I was wondering if this is possible without using replace function. Suppose my string is defined as ‘20220117A1745’ implying Jan. 17th, 2022 at …

Total answers: 2

How can I print a day and a time using Python strftime?

How can I print a day and a time using Python strftime? Question: how can I print a day and a time using Python strftime? My code is below, but any time I run it, I get "ValueError: time data ‘1 day, 2:45:00’ does not match format ‘%H:%M:%S’" . I want the output to be …

Total answers: 1

Converting datetime format maintaining the datetime data type in python

Converting datetime format maintaining the datetime data type in python Question: I need datetime object in the format mm/dd/yyyy. I tried using strptime: datetime.strptime("05-08-2022","%d-%m-%Y") >>datetime.datetime(2022, 8, 5, 0, 0) That changes the format to YYYY/MM/DD. I know we can change the format using strftime to convert datetime to any format but that results into a …

Total answers: 1

How to convert timestamp into string in Python

How to convert timestamp into string in Python Question: I have a problem with the following code. I get an error “strptime() argument 1 must be str, not Timestamp” I guess that what I should do is to convert date from timestamp to string but I do not know what to do. class TweetAnalyzer: def …

Total answers: 5

How to parse datetime that ends with `Z`?

How to parse datetime that ends with `Z`? Question: I have the following datetime string s: 2017-10-18T04:46:53.553472514Z I parese it like that: t = datetime.strptime(s, ‘%Y-%m-%dT%H:%M:%SZ’) how to fix ValueError: time data ‘2017-10-18T04:46:53.553472514Z’ does not match format ‘%Y-%m-%dT%H:%M:%SZ’ Asked By: user6611764 || Source Answers: I think you should use dateutil.parser module In [23]: s = …

Total answers: 5

How to parse timezone with colon

How to parse timezone with colon Question: Is there a way to parse timezone in “+00:00” format with datetime.strptime? For instance: Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In tel)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> from datetime import datetime >>> datetime.strptime(“12:34:56+0000”, “%X%z”) datetime.datetime(1900, 1, …

Total answers: 4

Converting strings in an array to dates

Converting strings in an array to dates Question: I have read an array of strings from a TXT file and saved it as an array (of over thousand values) using such a line: dates = np.genfromtxt(‘filename.txt’, delimiter=”;”, usecols=(0), dtype=None) Next, I would like to convert strings into dates. I tried to use the line: dates2 …

Total answers: 2

Display Python datetime without time

Display Python datetime without time Question: I have a date string and want to convert it to the date type: I have tried to use datetime.datetime.strptime with the format that I want but it is returning the time with the conversion. when = alldates[int(daypos[0])] print when, type(when) then = datetime.datetime.strptime(when, ‘%Y-%m-%d’) print then, type(then) This …

Total answers: 8