In python, how would I generate a timestamp to this specific format?
2010-03-20T10:33:22-07
I've searched high and low, but I couldn't find the correct term that describes generating this specific format.
See the following example:
import datetime
now = datetime.datetime.now()
now.strftime('%Y-%m-%dT%H:%M:%S') + ('-%02d' % (now.microsecond / 10000))
This could result in the following:
'2017-09-20T11:52:32-98'
You can use datetime with strftime. Exemple:
import datetime
date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
print(date)
Will print:
2017-09-20T12:59:43.888955
Related
I have some date data like:
46:53.4
57:00.0
51:50.9
53:13.9
What is this time format? And how to transfer it to the usual year-month-day-hour-minute-second in Python?
Code:
import datetime
#Input Date String
t = "46:53.4"
#Return a datetime corresponding to date string
dateTimeObject = datetime.datetime.strptime(t, '%M:%S.%f')
print (dateTimeObject)
Result:
1900-01-01 00:46:53.400000
I suppose 46:53.4 means forty-six minutes and fifty-three-point-four seconds.
I try to convert a date in english (2019-10-07) in french (07/10/2016)
I try
dat = '07/10/2019'
dat = time.strftime('%Y-%m-%d')
but got the result '2019-10-16' instead of '2019-10-07'
using datetime you can decide the format in which the source date is provided, and the target format you want.
from datetime import datetime
dat = '07/10/2019'
datetime.strptime(dat, "%d/%m/%Y").strftime("%Y-%m-%d")
out[6]: '2019-10-07'
strftime needs a time/date to convert, and it will use the current date and time if you don't provide one. The previous value of dat is not relevant - this information is not seen by strftime.
You need to provide the time information that strftime will format, as a tuple that you can get by parsing the original string. For this, use strptime (f for format, p for parse).
So:
dmy = '07/10/2019'
ymd = time.strftime('%Y-%m-%d', time.strptime(dmy, '%d/%m/%Y'))
# ^^^^^^^^ ^^^^^^^^
# output schema input schema
# now ymd is '2019-10-07'
(Or you can use the datetime module as in the other answer. This way, the parsing gives you an object, which has a method to format back - so you can write the whole operation "in order" on the line. But the general principle is the same: you need to parse, then format, and you need to specify the schema on each side.)
with :
dat = time.strftime('%Y-%m-%d')
you recover your actual date.
you need to make :
from datetime import datetime
dat = '07/10/2019'
dat = datetime.strptime(dat, '%m/%d/%Y')
print(dat.strftime('%Y-%m-%d') )
I'a m trying to extract only time (Hour:Minute) from datetime field
Example:
today_with_hour = fields.Datetime(
string=u'hora',
default=fields.Datetime.now,
)
I would like to know how get only hour from today_with_hour in format
17:10:20
This is one way to extract:
from datetime import datetime
now = datetime.now()
print(str(now.hour)+':'+str(now.minute)+':'+str(now.second))
This may be better way to do it
You can use strftime
Example:
from datetime import datetime
datetime.now().strftime("%H:%M:%S")
In your case you can follow like this:
from datetime import datetime
datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S").time()
Output will be:
17:28:52
Better way to do is by using strftime().
dt = datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S")
dt.strftime("%H:%M:%S")
output:
17:28:52
To get the current time from the datetime.now()
datetime.datetime.now().time().strftime("%H:%M:%S")
O/P:
'11:16:17'
if you want to get the time with milliseconds also, use isoformat()
datetime.datetime.now().time().isoformat()
O/P:
'11:20:34.978272'
ALL,
I'm trying to get a statistical data of the file. Doing so gives me following:
atime - datetime timestamp representation
atime_nano - nano-seconds resolution in addition to a_time.
What I'd like to do is to convert atime.atime_nano to a datetime variable in Python.
So if I have:
atime = 1092847621L
atime_nano = 7100000L
I'd like to convert it to the datetime object in python that will have correct date with the milliseconds.
How can I do that?
Thank you.
Datetimes can have microseconds (1 microsecond = 1000 nanoseconds)
You can do the following for your example:
dt = datetime.fromtimestamp(1092847621L).replace(microsecond = 7100000L/1000)
I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated
from datetime import date
today=date.today()
print today
You can use today.strftime(format). format will be a string as described here http://docs.python.org/library/time.html#time.strftime . Example:
from datetime import date
today = date.today()
today.strftime("%x")
#>>> '01/31/11'
datetime.strftime allows you to format a datetime however you want
stringDate = str(today)
If that's what you want.
In Python3, you can use f-string formatting:
from datetime import date
today=date.today()
today_str = f"{today:%m/%d/%y}"
today_str
#>>>'06/24/22'