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

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"

Related

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

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')))

How do I get the full timezone name from a Datetime without importing Time?

I'm trying to display my full time zone along with my current local time.
print "Your current time is {0} {1} ".format(datetime.datetime.now().time(), time.tzname[0])
And the result would look something like:
Your current time is 08:35:45.328000 Pacific Standard Time
The problem is I have to import the Time library (sorry if I call it wrong, I'm coming from C#) along with the Datetime library.
import datetime
import time
I've looked into the naive and aware states of time, but still can't seem to get the desired result.
Is there a way to get the full timezone name (i.e.: Pacific Standard Time) from Datetime without having to import Time?
The example for writing your own local timezone tzinfo class uses (scroll down one page from here) pulls the tzname from time.tzname, so Python doesn't have a better built-in solution to suggest.
You could copy that example LocalTimezone implementation, which would allow the use of the %Z format code for strftime on an aware datetime instance using the LocalTimezone, but that's the best you can do with the built-ins. On a naive datetime the tzname is the empty string, so you need an aware datetime for this to work.

keep timezone "CET" from convert into "CEST" in python

If I use the following command to localize my timestamp to "CET":
pd.Timestamp('2011-11-06 11:00:00').tz_localize('CET')
output: Timestamp('2011-11-06 11:00:00+0100', tz='CET')
if I use :
pd.Timestamp('2011-06-06 11:00:00').tz_localize('CET')
output: Timestamp('2011-06-06 11:00:00+0200', tz='CET')
So obviously it automatically convert into "CEST" in summer, am I right? what if I would like to keep my timestamp in "CET" through out the whole year (keep it UTC +1:00 all the time), what should I do? Thank you very much!
Updated:
the following seems working:
pd.Timestamp('2011-06-06 11:00:00').tz_localize(tz=pytz.FixedOffset(60))
output:Timestamp('2011-06-06 11:00:00+0100', tz='pytz.FixedOffset(60)')
any other suggestions?

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