How to convert string to timestamp? [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert Date String to DateTime Object in Python
I have a date string:
Mon Oct 15 15:05:00 UTC 2012
How to converte this string to timestamp ?

This should help:
http://docs.python.org/library/time.html#time.strptime

Related

Convert a number sequence into a date using python? (example: 221201) [duplicate]

This question already has answers here:
How can I convert a string into a date object and get year, month and day separately?
(4 answers)
Closed 2 months ago.
I have a bunch of number sequences like "221201" meaning the year 2022, month 12 and day 01 (so 2022-12-01). How can I convert number sequences in this format into the actual date using python?
I've tried using the dateutil library but couldn't figure out how to get it to recognize this format.
from datetime import datetime
date_str = '221201'
date_obj = datetime.strptime(date_str, '%y%m%d')
print(type(date_obj))
print(date_obj) # printed in default format
You can learn more about strptime in this link

How can I change date stamps in python [duplicate]

This question already has answers here:
How to Convert Datetime to String in Python?
(3 answers)
How do I turn a python datetime into a string, with readable format date?
(8 answers)
Convert datetime object to a String of date only in Python
(15 answers)
Closed 9 months ago.
how can I convert the date format from
Current format : [day] [month] [day value] [hour]:[minute]:[second] [time zone difference] [year]
to
New format : [year]-[month value]-[day value] [hour]:[minute]:[second]
For example, a current format value:
Tue Feb 04 17:04:01 +0000 2020
should be converted to:
2020-02-04 17:04:01
in python
You can use parser. Then use strftime to format the datetime object
from dateutil import parser
x = 'Tue Feb 04 17:04:01 +0000 2020 '
y = parser.parse(x).strftime('%Y-%m-%d %T')
>>> y
'2020-02-04 17:04:01'
Check out:
http://strftime.net/

I need to convert string to date in python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I need to convert string to date type.What should I do?
My string is "04 Oct 2019". I need to convert to date type "04/10/2019".
Note:You can't ignore 0
import calendar
def convert_str_to_date(s):
l = s.split()
l[1] = "{0:02d}".format(list(calendar.month_abbr).index(l[1]))
return "/".join(l)
print(convert_str_to_date("04 Oct 2019"))
It won't ignore 0 as "{0:02d}".format

Converting Datetime string with non-zero padded day [duplicate]

This question already has answers here:
Whats wrong with my datetime.strptime format?
(1 answer)
datetime.strptime strange behavior [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to convert a string to datetime object in python. Some examples of the string includes "Fri Oct 20 2006 4:25pm EDT" and "Wed Nov 1 2006 4:47pm EST" The problem seems to be that the day is not zero-padded which is why datetime does not pick up the format
I have also tried slicing it to convert the first 15 characters only but it seems that the non-zero padded dates are still a problem.
import datetime
for i, row in df.iterrows():
datetime_list.append(datetime.datetime.strptime(row['Time Stamp'],
'%a %b %d %Y %I:%M%p %Z'))
I hope to get a datetime object out that only shows the year, month and date.

Convert string into datetime timestamp [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
I have a string '201502190759'. This represents 2015/02/19 at 7:59.
Is there a method within the python library that can convert '201502190759' into a datetime object or timestamp?
Yep, datetime.datetime.strptime will do it.
>>> import datetime
>>> print(datetime.datetime.strptime('201502190759', '%Y%m%d%H%M'))
2015-02-19 07:59:00
The docs on the format modifiers are here.

Categories