how to convert todays datetime into timestamp - python

I am tryng to convert date-time into string.
I am searching answer of this question
when i do this
time.mktime(time.strptime('2017-05-01 14:07:19', '%Y-%m-%d %H:%M:%S'))
i am able convert datetime into time-stamp but I want to concert today's date into time-stamp like this:
timenow = datetime.datetime.now()
//timenow = 2017-05-01 14:07:19
time.mktime(time.strptime(timenow, '%Y-%m-%d %H:%M:%S'))
then it throws error TypeError: expected string or buffer even i tried like
time.mktime(time.strptime(str(timenow), '%Y-%m-%d %H:%M:%S'))
then it throws ValueError: unconverted data remains: .067000
How could i convert todays datetime into timestamp

import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.timestamp())

Related

How to creat one type of datetime DF format in excel [duplicate]

This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
Closed 9 months ago.
I have
5/7/2022 12:57(m/d/yyy)
5/7/2022 13:00 PM(m/d/yyy) time formats.
There are two types of time formats in a column of excel file which I have downloaded.
I want to convert it to '%Y-%m-%d %H:%M:%S'.
(The column is in string format).
I guess you have your file loaded from excel to dataframe.
df['date_col'] = pd.to_datetime(df['date_col'], format='%Y-%m-%d %H:%M:%S')
from dateutil.parser import parse
datestring = "5/7/2022 12:57"
dt = parse(datestring)
print(dt.strftime('%Y-%m-%d %H:%M:%S')) #2022-05-07 12:57:00
You can turn string input to datetime by doing this:
from datetime import datetime
example1 = "5/7/2022 12:57"
example2 = "5/7/2022 13:00 PM"
datetime_object1 = datetime.strptime(example1, "%m/%d/%Y %H:%M")
datetime_object2 = datetime.strptime(example2, "%m/%d/%Y %H:%M %p")
and then you can represent the datetime variable with a string:
formatted_datetime1 = datetime_object1.strftime("%Y-%m-%d, %H:%M:%S")
formatted_datetime2 = datetime_object1.strftime("%Y-%m-%d, %H:%M:%S")
You can try using pandas.Series.dt.strftime method, that will allow you to convert a field into the specified date_format, in this case %Y-%m-%d %H:%M:%S.
df['Column'] = df['Column'].dt.strftime('%Y-%m-%d %H:%M:%S')

How to fix date formatting using python3

I have data with the date format as follows:
date_format = 190410
year = 19
month = 04
date = 10
I want to change the date format, to be like this:
date_format = 10-04-2019
How do I solve this problem?
>>> import datetime
>>> date = 190410
>>> datetime.datetime.strptime(str(date), "%y%m%d").strftime("%d-%m-%Y")
'10-04-2019'
datetime.strptime() takes a data string and a format, and turns that into datetime object, and datetime objects have a method called strftime that turns datetime objects to string with given format. You can look what %y %m %d %Y are from here.
This is what you want(Notice that you have to change your format)
import datetime
date_format = '2019-04-10'
date_time_obj = datetime.datetime.strptime(date_format, '%Y-%m-%d')
print(date_time_obj)
Here is an other example
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
You can also do this
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print(date)
There are many ways to achieve what you want.

Create a datetime from a string representation in a CSV file

I have a CSV file with recorded datetimes with a particular format:
%Y-%m-%d %H:%M:%s %Z
Example:
2017-02-11 14:11:42 PST
I am trying to format the datetime to a friendlier value to use later on.
However, I have been unable to create a datetime object with my code so far.
Here is my code:
for r in row:
purchase_date.append(
datetime.strptime(row['purchase-date'], "%Y/%m/%d %H:%M:%S %Z")
)
This is the error received:
ValueError: time data '2017-02-11 14:11:42 PST' does not match format %Y/%m/%d %H:%M:%S %Z'
Timezones are often rather wonky when trying to convert from a string. It is often best to deal with the timezone string yourself. Here is a bit of code which separates the timezone from the timestamp, and then converts them separately.
Code:
import datetime as dt
import pytz
my_timezones = dict(
PST='US/Pacific',
)
def convert_my_datetime_str(dt_str):
# split into time and timezone
timestamp, tz_str = dt_str.rsplit(' ', 1)
# convert the date string to datetime
time = dt.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
# get a timezone name
tz = pytz.timezone(my_timezones[tz_str])
# return a timezone aware datetime
return tz.localize(time)
Test Code:
print(convert_my_datetime_str('2017-02-11 14:11:42 PST'))
Results;
2017-02-11 14:11:42-08:00
You should be able to just change the format to match your date strings. In the error, your date string has dashes instead of slashes, so make the format string match:
for r in row:
purchase_date.append(
datetime.strptime(row['purchase-date'], "%Y-%m-%d %H:%M:%S %Z")
)

how do you format date time in python

I have a variable that has value like this:
val='14/12/15 0000'
it is in two digit year/month/day hourminute format.
I need to convert this to epoch time.
I tried this
import datetime
datetime.datetime.strptime(val, "%y/%m/%d %HH%MM").strftime('%s')
I get this error:
ValueError: time data '14/12/15 0000' does not match format '%y/%m/%d %HH%MM'
what am I doing wrong here?
Hours (24 hr) are %H, not %HH, and minutes are %M, not %MM.
datetime.datetime.strptime(val, "%y/%m/%d %H%M").strftime('%s')
You can use easy_date to make it easy:
import date_converter
my_datetime = date_converter.string_to_string('14/12/15 0000', '%y/%m/%d %H%M', '%s')
Or even convert directly to a timestamp:
import date_converter
timestamp = date_converter.string_to_timestamp('14/12/15 0000', '%y/%m/%d %H%M')

Datetime from strings with microseconds

How to include microseconds to datetime, for example:
import datetime
dt = datetime.datetime.strptime('2010-08-30 15:02:55.730', '%Y-%m-%d %H:%M:%S')
>>> ValueError: unconverted data remains: .730
I want to have microseconds too. What should format string look like? What is the placeholder for microseconds?
dt = datetime.datetime.strptime('2010-08-30 15:02:55.730', '%Y-%m-%d %H:%M:%S.%f')
you can look at:
http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Categories

Resources