How do I find date for with hours? - python

In python, I want to find the date and time and put it inside of a string.
I have tried this:
example = datetime.datetime.now()
print(example)
However it returns the date with the miliseconds. What do I do if i don't want the miliseconds included. Just the date and time formatted like this:
YYYY-MM-DD 00:00:00

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

You need to format the datetime output using this:
datetime.datetime.now().strftime('%H:%M:%S')
This will give you Hours:Minutes:Seconds
To get the format as per your requirements use:
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
This will give you date time in YYYY-MM-DD 00:00:00 format without milliseconds.
My output: 2019-11-04 22:09:06
To see more directives: Link
Link says:
You can get the formatted date and time using strftime function. It
accepts a format string that you can use to get your desired output

If I understand your question corretly, somethinig like this should work for you.
from datetime import datetime
now = datetime.now()
date = now.strftime("%m/%d/%Y")
print("date:",date)
It came from here. There are other formatting examples in that post.

Datetime's strftime functionality does exactly what you're looking for.
There is some overview and explanation here:
https://www.programiz.com/python-programming/datetime/strftime
print(example.strftime('%Y-%m-%d %H:%M:%S')) will give you the right answer.
I would also look at the arrow library, as it offers a nice API to work with time.

Related

Converting string to datetime with strptime in Python

I'm trying to convert a string to datetime with dd/mm/YYYY format, but the function "strptime" seems isn't working properly. The result format isn't in the same that i am specifying in the function.
import datetime
from datetime import datetime
from datetime import date
today = date.today()
print (today)
today = today.strftime("%d-%m-%Y %H:%M:%S")
print (today)
today = datetime.strptime(today, "%d-%m-%Y %H:%M:%S")
print (today)
My output is this:
2022-08-26
26-08-2022 00:00:00
2022-08-26 00:00:00
it seems that after the strftime function, the format is right, but when i call the strptime function, its getting back to the previous format "YY-mm-dd"
today = datetime.strptime(today, "%d-%m-%Y %H:%M:%S")
print (today)
That part is creating a datetime object.
You can do:
print (today.strftime("%d-%m-%Y %H:%M:%S"))
again in the final line
it seems that after the strftime function, the format is right, but when i call the strptime function, its getting back to the previous format "YY-mm-dd"
That is because print(today) determines the format since you didn't specify one. As other's pointed out, today is a datetime object. When you print() a datetime object, python will choose a format for you, I assume based on your system's locale settings.
One concept to understand here is the difference between a value and the representation of that value as a string. This is what formatting a datetime is for.
I suggest you don't worry about the output here too much. As long as your explicit format with strftime() gives you the correct representation, then you are good. This formatting should only be used for output. Otherwise, store datetimes and datetime objects in your variables.

how do i get only the time in python without the date and year?

I need to get time in python and i am using time.ctime() and it returns this Thu Jul 8 15:37:26 2021
But i only need 15:37:26 and i cant figure out how to get only this and not the date and year.
I already tried using datetime where i could not figure it out either so im trying with time now.
here is a bit of code for the context:
cas = time.ctime()
cas = str(cas)
api.update_status('nyni je:'+ cas )
time.sleep(60)
Anyone know how to do it?
print(datetime.datetime.now().time().isoformat(timespec='seconds'))
import datetime
print(datetime.datetime.now().strftime("%H:%M:%S"))
imports
from datetime import datetime
code
now = datetime.now()
cas = now.strftime("%H:%M:%S")
print(cas)
You can use strftime to convert a datetime value to a string of a certain format. In your case you can use %H:%M:%S to only get the time. The same function can be used to get the date as well, you can read more here.
Take a look at the "strftime() and strptime() Format Codes" section also for how you can format it.

How to convert timestamp to HH:MM:SS format using python and graph the information?

I am trying to use timestamp information to graph.
However, when I convert the number into hh:mm:ss. It does not work.
I have tried this:
timestamp = [dt.strptime(str(datetime.timedelta(seconds=round(t/1000))),'%H:%M:%S') for t in timestamp1]
Also I tried this
timestamp = [dt.strftime(dt.strptime(str(datetime.timedelta(seconds=round(t / 1000))), '%H:%M:%S'), '%H:%M:%S') for t in timestamp]
However, it is possible to see the list with the new values. However, I have problems with the graphs and these new values.
Can anybody help me?
Try using this and make sure to import the time module!
timestamp = [time.strftime('%H:%M:%S', time.gmtime(t/1000)) for t in timestamp1]
In your code you're using datetime.strptime(date_string, format) function, it's main use is to parse time strings in order to create datetime object.
In order to get a parsed string of a desired format you should use date.strftime(format) function instead.
So basically in your code you can only add it and should get the wanted result:
import datetime
from datetime import datetime as dt
timestamp = [dt.strftime(dt.strptime(str(datetime.timedelta(seconds=round(t / 1000))), '%H:%M:%S'), '%H:%M:%S') for t in timestamp1]
I'm not sure what was your input and desired output, but you could also use date.fromtimestamp(timestamp) for the parsing of timestamps

Python: How to display date time in following format (2018-06-25T07:17:17.000Z)?

I want to display the datetime in the following format using python:
2018-06-25T07:17:17.000Z
I am trying to convert using strftime:
datetime.datetime.now().strftime("%Y-%m-%dTH:M:SZ")
but it seems that doesn't work.
What i am missing?
you can use
import datetime
datetime.datetime.today().isoformat()
2018-06-25T07:17:17.000Z
This format is called ISO format, after standard ISO 8601. The datetime object has a isoformat method to output this form.
strftime("%Y-%m-%dTH:M:SZ")
You seem to have forgotten some % before the H, M, and S. Try strftime("%Y-%m-%dT%H:%M:%SZ").
but it seems that doesn't work.
Generally it works better if you specify exactly what doesn't work, or what you expect and how the reality differs from your expectation.
You can use following formating for date conversion.
>>> import datetime
>>> today_date = datetime.datetime.now()
>>> today_date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
'2018-06-25T15:50:18.313620Z'
Please let me know,if this is the one you needed.

Python Convert a Date Time to just Time

I have a field that shows time as 1900-01-01 00:05:00 but I want to show it as just 00:05:00 i.e. just five minutes.
I have been looking at the python documentation and it is driving me crazy. From the question How do I find the time difference between two datetime objects in python? it looks like is should be something do with timedelta but I am not getting any closer to a solution. The question Converting Date/Time to Just Time suggests you can just use a format tag but that is not working for me. I also looked at converting date time to string without success, I know this is something simple but I have looked at hundreds of pages looking for something simple. All help would be appreciated.
Say you have a Datetime object now:
now.time().strftime('%H:%M:%S')
Load the string with strptime() and get the time() component:
>>> from datetime import datetime
>>> s = "1900-01-01 00:05:00"
>>> dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
>>> dt.time().isoformat()
'00:05:00'
Here we are dumping the time with isoformat() in ISO 8601 format, but you can also dump the datetime.time back to string with strftime():
>>> dt.time().strftime("%H:%M:%S")
'00:05:00'

Categories

Resources