Python: How to print date format with using the help function? - python

I would like to print the date format so I dont need to search in the navigator every time I want to print a date like in the following code:
time = now.strftime("%Y-%m-%d %H:%M:%S")
print("time:", time)
I been searching and didn't find any thing about this.
When you run help(the_date.strftime), it doesn't show the possible parameters.

You can see the format codes here - Basic date and time types

The below sample reference:
import time
print("time:{}".format(time.strftime('%Y-%m-%d %H:%M:%S')))

Related

How do I add AM/PM to adatetime.strftime module?

I've incorporated a datetime module that gives the output of the current time, but it does not include AM/PM in the output. Specifically I am using the "Now" and "strftime" features to give the exact time in the output. I am wondering what else I might need to add in order for Am or PM to show but I could not find anything to figure it out. Any help is appreciated.
import datetime
my_datetime = datetime.datetime.now()
print("Checkout at:", my_datetime.strftime("%Y-%m-%d %H:%M:%S"))
For reference, the output right now looks like this:
"Checkout at: 2022-07-28 01:12:07"

BeautifulSoup: Get the time and date of a column that shows "x minutes ago"

I'm trying to scrape some info of this website: https://cryptoslam.io/nba-top-shot/sales As you can see, in the fist column, 'Sold', I get some information of the time and date of the row. The problem is in this column this information is shown by text with like "2 minutes ago", "x seconds ago" or "six months ago".
I would like to create a dataframe where the column 'Sold' shows the exact time and date. Instead of "one minute ago" I would like to get "11-3-2021, 17:17h" or something like this. Is this possible?
Thanks in advance.
BeautifulSoup won't do this for you itself. It's responsible for pulling whatever is on the page out of the page for you. It doesn't deal with any sorts of data conversions. So you will need a module that parses these strings and converts them to date/time values.
I found the module that will do this for you. You can install it with:
pip install dateparse
Then it's super simple to use it:
import dateparser
import datetime
# Display current time
now = datetime.datetime.now()
print(now)
# Display result of parsing a human-readable relative time
# to go back two hours
date = dateparser.parse('2 hours ago')
print(date)
Result:
2021-03-11 09:05:05.884789
2021-03-11 07:05:05.942474

How to get the Date and Time of Current Location in Python [duplicate]

This question already has answers here:
How do I get the current time?
(54 answers)
Closed 3 years ago.
I want to get the Date and Time of The Current Location. If somebody runs the code in a different country thought it will show the date and and time for them, and not for me. If you know any commands for this please put the down bellow. I prefer If I don't need a library so that people who get the code don't need to download the library.
I think you can not get the date or time of a location.
This code gets your device's date and time:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%D")
print("Current Time: ", current_time)
print("Current Date: ", current_date)
A standard Python module called datetime can probably help you with retrieving the current system date and time from where the program is run
Since it's a standard module, there would be no need to install it rather just import it in the program itself.
Example :
import datetime
x = datetime.datetime.now()
print(x)
Would give you the output as :
2019-07-20 00:52:27.158746
The above output contains the current year, month, day, hour, minute, second, and microsecond.

Converting "2013-01-06T22:25:08.733" to "2013-01-06 22:25:08" in python

I have a big .csv file which holds machine log data. One of the fields is timestamp. It stores date and time as shown in the title and I would like to drop the milli seconds and convert it into the format also shown in title. Can anyone help me with that? Im new to Python and Ipython.
Much obliged.
For your special case, this should suffice:
t.replace('T', ' ')[:19]
But I would recommend, that you use the datetime module of the standard library instead, so your time conversion also could be internationalized.
You can use easy_date to make it easy:
import date_converter
new_date = date_converter.string_to_string("2013-01-06T22:25:08.733", "%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S")

python getting weekday from an input date

I'm trying to work on a homework problem where I have an input date in the format YYYY-MM-DD
I need to import a couple modules and create a function weekday where it splits up the date and returns the weekday.
So far I imported:
from time import *
from datetime import *
I need help in my weekday function where I must use a .split method
Create an object of the class datetime.date.
and use strftime to return the day of the week in full text.
Can anyone help me get started on how to implement these functions and modules properly?
Suppose s is your input string:
s = '2010-10-29'
At the prompt, try s.split('-'). What happens?
Create a date object:
d = datetime.date(2010, 10, 29)
Then run d.strftime('%B'). What happens? How would you get the weekday, instead?
You can fill in the rest. Look at the Python docs for more information. Python doc: time, datetime
it's two lines
import time
print time.strftime("%A", time.strptime('2010-10-29', "%Y-%m-%d"))
prints 'Friday'

Categories