How to format time.time() using strftime() in python [duplicate] - python

This question already has answers here:
Convert timestamp since epoch to datetime.datetime
(3 answers)
Closed 3 years ago.
I have an epoch time that is in Seconds.PartOfSecond format e.g. 1581218900.17436. How can this be used within time.strftime() ?
import time
curTime = time.time()
# Following is not correct but gives idea what i'm looking for
formattedTime = time.strftime('%A %B %e, %Y %t', curTime)
Update The answer this has been closed for is not imo an obvious replacement: how would one know we need to convert to datetime.datetime? The answer below is more to the point.

You need pass your second to gmtime
import time
s=time.gmtime(1581218900.17436)
time.strftime("%Y-%m-%d %H:%M:%S", s)
'2020-02-09 03:28:20'
time.strftime('%A %B %e, %Y %t', s)
'Sunday February 9, 2020 \t'

Related

How do I turn variable into converted time/date? [duplicate]

This question already has answers here:
How to convert milliseconds to date and time format?
(2 answers)
Closed 4 months ago.
I have a variable named "timestamp" and the value of it is "1617108899460", how would I turn this into this format: month/day/year, hour:minutes:seconds
I tried to do the following but I got an error:
date_time = datetime.fromtimestamp(timestamp).strftime("%A, %B %d, %Y %I:%M:%S")
OSError: [Errno 22] Invalid argument
Is there any way I can do this in a simple way? Like with a module?
Duplicate of this. Your timestamp value is in milliseconds.
from datetime import datetime
timestamp = 1617108899460
date_time = datetime.fromtimestamp(timestamp / 1e3).strftime("%A, %B %d, %Y %I:%M:%S")
This works as expected.

Python dateutil returns YYYY-MM-DD, but I need it reversed , help please? [duplicate]

This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 4 years ago.
I've got a piece of script in python that parses a line containing date and time:
d = dateutil.parser.parse("%s %s" % (m.group('date'),
m.group('time')), dayfirst=True)
it returns the date in YYYY-MM-DD. How can I get it to say Weekday DD Month YYYY? Or if that is not possible DD-MM-YYYY?
Additionally, it also gives the time in HH:MM:SS, but I only need HH:MM, is there a way to lose the seconds?
Thank you!
I'm a complete novice to python so hope that anyone can help me, thank you very much!
Use datetime module's strftime to change the format according to the docs.
d = '2019-01-09'
d = datetime.datetime.strptime(d, '%Y-%m-%d')
print(d)
d = d.strftime('%A %d %B %Y %H:%M') # %I:%M if you want 12 hour format
print(d) # Wednesday 09 January 2019 00:00
from datetime import date, timedelta
day = date.today() # this gives you output in default YYYY-MM-DD format
now = datetime.now() # this give your output in default YYYY-MM-DD hh:mm:ss.millisec format
timestamp = now.strftime('%d-%m-%Y-%H:%M') # this gives you the output in the format you are looking for. See the screenshot
Have a look at this link for further details: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Days between dates [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
How do I parse an HTTP date-string in Python?
(4 answers)
Closed 5 years ago.
I need count days to password change in linux.
I know how to do this in python e.g:
>>> import datetime
>>> start = datetime.date(2016,1,1)
>>> end = datetime.date(2016,2,28)
>>> end-start
But my date format is:
Oct 03, 2017
How I can calculate days to a date?
You can do this using strftime().
It is actually really simple:
from datetime import datetime
d1 = datetime.strptime("Jan 01, 2016", '%b %d, %Y')
d2 = datetime.strptime("Feb 28, 2016", '%b %d, %Y')
print "Delta (in days):", (d2-d1).days
And you'll get 58 as a result.

How to convert a string to datetime object [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 5 years ago.
With the time value being:
value = '2017-08-31T02:24:29.000Z'
I try to convert it to a datetime object with:
import datetime
datetime_object = datetime.datetime.strptime(value, '%Y-%b-%d %I:%M%p')
But the command crashes with the exception:
ValueError: time data '2017-08-31T02:24:29.000Z' does not match format '%Y-%b-%d %I:%M%p'
You should be using a builtin Python's datautil module instead of date time:
import dateutil.parser
value = '2017-08-31T02:24:29.000Z'
result = dateutil.parser.parse(value)
First of all, you are missing the formatter for the microsecond.
Second of all, there is no second colon for dividing the minute and second.
Third, the %b operator is for the monthname (Jan,Feb,etc.). You want to use %m.
Final format is '%Y-%m-%dT%I:%M:%S.%fZ'.
This is your code:
datetime_object = datetime.datetime.strptime(value, '%Y-%m-%dT%I:%M:%S.%fZ')
You should get 2017-08-31 02:24:29 as the value of datetime_object.

Parse english textual date expression into datetime [duplicate]

This question already has answers here:
Natural language time parser [closed]
(2 answers)
Closed 9 years ago.
How do I convert these strings:
one hour ago
three days ago
two weeks ago
yesterday
next month
into Python datetime object?
Just found parsedatetime for parsing human readable date/time text from the link provided by Jon Clements. Here is a solution in case you interested:
from time import mktime
from datetime import datetime
import parsedatetime as pdt
time_str = '1 hour ago'
cal = pdt.Calendar()
dt = datetime.fromtimestamp(mktime(cal.parse(time_str)[0]))
time_formatted = dt.strftime('%b %d, %Y %H:%M')
print(time_formatted) # will print something like: Dec 15, 2013 02:10
Also see this question: python 'x days ago' to datetime

Categories