I would like the first datetime in the hyperlink to be 1 day before the second which is today's date. I read a little bit about the timedelta but I did not see how it applied within a hyperlink.
http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=skatersummary&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%22' + datetime.datetime.now().strftime('%Y-%m-%d') + '%22%20and%20gameDate%3C=%22' + datetime.datetime.now().strftime('%Y-%m-%d') + '%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22
Some well placed parentheses should suffice:
(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
A friendly suggestion: take a look at Python string formatting instead of constructing your string using concatenation. It'll end up being a lot cleaner and less repetitive.
I needed to label files with a header that was 1 day prior to execution so this worked for me:
from datetime import date, timedelta, datetime
header = (datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d")
so that "header" became my string
For class datetime.timedelta the valid arguments are:
days=0
seconds=0
microseconds=0
milliseconds=0
minutes=0
hours=0
weeks=0
All arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative. Only days, seconds and microseconds are stored internally. Arguments are converted to those units:
A millisecond is converted to 1000 microseconds.
A minute is converted to 60 seconds.
An hour is converted to 3600 seconds.
A week is converted to 7 days.
For strftime()
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a decimal number. (Platform specific) 30
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. September
%m Month as a zero-padded decimal number. 09
%-m Month as a decimal number. (Platform specific) 9
%y Year without century as a zero-padded decimal number. 13
%Y Year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 07
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 7
%I Hour (12-hour clock) as a zero-padded decimal number. 07
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 000000
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z Time zone name (empty string if the object is naive).
%j Day of the year as a zero-padded decimal number. 273
%-j Day of the year as a decimal number. (Platform specific) 273
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 39
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 39
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 07:06:05
%% A literal '%' character. %
Related
How can I convert a date into a numeric date?
For example, I want to convert '06-Jun-2021' to '20210609' and then turn it into a string i can use in a webpage eg. baseball.theater/games/20210609 so that i can automate the process daily.
using datetime i've managed to do :
print (todays_date.year,todays_date.month,todays_date.day,sep="")
which can print the output i need (without the trailing 0's) but i cannot make this into a string which i can use.
obviously i am a COMPLETE newcomer to python, so be gentle please.
You can use datetime.strptime to turn a string into a datetime object, then datetime.strftime to reformat it into a different string.
>>> from datetime import datetime
>>> s = '06-Jun-2021'
>>> dt = datetime.strptime(s, '%d-%b-%Y')
>>> dt.strftime('%Y%m%d')
'20210606
For the specific case of the current day, you can use datetime.today
>>> datetime.today().strftime('%Y%m%d')
'20210609'
To combine this into your final string you can use str.format
>>> 'baseball.theater/games/{}'.format(datetime.today().strftime('%Y%m%d'))
'baseball.theater/games/20210609'
Here, just typecast it to str and use .replace() method
from datetime import date # datetime is a built-in module
today = str(date.today())
string = today.replace("-", "")
print(string)
P.S Just providing an alternate method to strftime
I think you are looking time.strftime()
The function needs time module
import time
then you can either use a variable and display that time in a specific format.
t = time.time()
print(time.strftime('%H%M%S', t) # print the time t in specific format
print(time.strftime('%H%M%S') # print present time in specific format
Here is a list of options from https://www.tutorialspoint.com/
%a - abbreviated weekday name
%A - full weekday name
%b - abbreviated month name
%B - full month name
%c - preferred date and time representation
%C - century number (the year divided by 100, range 00 to 99)
%d - day of the month (01 to 31)
%D - same as %m/%d/%y
%e - day of the month (1 to 31)
%g - like %G, but without the century
%G - 4-digit year corresponding to the ISO week number (see %V).
%h - same as %b
%H - hour, using a 24-hour clock (00 to 23)
%I - hour, using a 12-hour clock (01 to 12)
%j - day of the year (001 to 366)
%m - month (01 to 12)
%M - minute
%n - newline character
%p - either am or pm according to the given time value
%r - time in a.m. and p.m. notation
%R - time in 24 hour notation
%S - second
%t - tab character
%T - current time, equal to %H:%M:%S
%u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
%U - week number of the current year, starting with the first Sunday as the first day of the first week
%V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
%W - week number of the current year, starting with the first Monday as the first day of the first week
%w - day of the week as a decimal, Sunday=0
%x - preferred date representation without the time
%X - preferred time representation without the date
%y - year without a century (range 00 to 99)
%Y - year including the century
%Z or %z - time zone or name or abbreviation
%% - a literal % character
This question already has an answer here:
Python format date using only string format() method
(1 answer)
Closed 3 years ago.
I need help to convert this string '20190625091115' to timestamp '25-06-2019 09:11:15' in python. Format in 'YYYYMMDDHHMMSS > Format out 'DD-MM-YYYY HH:MM:SS'.
First part would be creating datetime object:
from datetime import datetime
date_string = "20190625091115"
format_date = datetime.strptime(date_string, '%Y%m%d%H%M%S'))
After which format date is:
print(format_date)
2019-06-25 09:11:15
Little clarification here, on python reference page, you can see definition for '%Y%m%d%H%M%S') format specifiers I used.
%Y: Year with century as a decimal number, e.g. 1970, 1988, 2001, 2013
%m: Month as a zero-padded decimal number (e.g. 01, 02, ..., 12)
%d: Day of the month as a zero-padded decimal number (e.g. 01, 02, ..., 31)
%H: Hour (24-hour clock) as a zero-padded decimal number (e.g 00, 01, ..., 23)
%M: Minute as a zero-padded decimal number (e.g 00, 01, ..., 59)
%S: Second as a zero-padded decimal number (e.g. 00, 01, ..., 59)
%f: Microsecond as a decimal number, zero-padded on the left (000000, 000001, ..., 999999)
%z: UTC offset in the form +HHMM or -HHMM, empty string if the the object is naive, (empty or +0000, -0400, +1030)
I have a datetime object,
import time, datetime, pytz
current_unixtime = time.time()
current_date_milis_for_blibli = int(round(current_unixtime * 1000))
current_datetime_object = datetime.datetime.fromtimestamp(current_unixtime, pytz.timezone('Asia/Jakarta'))
how do i convert it into:
Mon May 16 14:07:15 WIB 2016
or in PHP equivalence:
D M d H:i:s T Y
What i tried are written below, as you can see, i can't seem to get the 3 characters for Day and Month:
year = current_datetime_object.year
month = current_datetime_object.month
day = current_datetime_object.day
hour = current_datetime_object.hour
minute = current_datetime_object.minute
second = current_datetime_object.second
result = current_datetime_object.strftime("%a %b %d %H:%M:%S %Z %Y")
You can also specify output by changing values in brackets.
Examples are based on datetime.datetime(2013, 9, 30, 7, 6, 5).
Code Example Meaning
%a Mon # Weekday as locale’s abbreviated name.
%A Monday # Weekday as locale’s full name.
%w 1 # Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
%d 30 # Day of the month as a zero-padded decimal number.
%-d 30 # Day of the month as a decimal number. (Platform specific)
%b Sep # Month as locale’s abbreviated name.
%B September # Month as locale’s full name.
%m 9 # Month as a zero-padded decimal number.
%-m 9 # Month as a decimal number. (Platform specific)
%y 13 # Year without century as a zero-padded decimal number.
%Y 2013 # Year with century as a decimal number.
%H 7 # Hour (24-hour clock) as a zero-padded decimal number.
%-H 7 # Hour (24-hour clock) as a decimal number. (Platform specific)
%I 7 # Hour (12-hour clock) as a zero-padded decimal number.
%-I 7 # Hour (12-hour clock) as a decimal number. (Platform specific)
%p AM # Locale’s equivalent of either AM or PM.
%M 6 # Minute as a zero-padded decimal number.
%-M 6 # Minute as a decimal number. (Platform specific)
%S 5 # Second as a zero-padded decimal number.
%-S 5 # Second as a decimal number. (Platform specific)
%f 0 # Microsecond as a decimal number, zero-padded on the left.
%z # UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z # Time zone name (empty string if the object is naive).
%j 273 # Day of the year as a zero-padded decimal number.
%-j 273 # Day of the year as a decimal number. (Platform specific)
%U 39 # Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
%W 39 # Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
%c Mon Sep 30 07:06:05 2013 # Locale’s appropriate date and time representation.
%x 09/30/13 # Locale’s appropriate date representation.
%X 07:06:05 # Locale’s appropriate time representation.
%% % # A literal '%' character.
Example is taken from here
Use the datetime module:
import datetime
datetime.datetime.now().strftime('%a %B %d %H:%M:%S %Z %Y')
I can find the answer to the question here, turns out Python Documentation mentioned this kind of conversion between datetime object to a formatted string:
https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
The following date is returning a value error for what appears to be a valid datetime string. Why?
from datetime import datetime
dateFormat = "%A %b %d, %Y %I:%M %p"
myDateStr = "Sunday May 22, 2016 00:47 AM"
try:
date_object = datetime.strptime(myDateStr,dateFormat)
print(date_object)
except ValueError as e:
print(e)
I will admit to being slightly confused. It's been tested on two platforms and does generally work.
Thanks
For the benefit of the reader here are the format masks.
%A Weekday as locale’s full name. Sunday, Monday, ..., Saturday (en_US);
%b (%B) Month as locale’s abbreviated name. Jan, Feb, ..., Dec (en_US);
%d Day of the month as a zero-padded decimal number, 01, 02, ..., 31
%Y Year with century as a decimal number. 1970, 1988, 2001, 2013
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
%M Minute as a zero-padded decimal number. 00, 01, ..., 59
%p Locale’s equivalent of either AM or PM. AM, PM (en_US);
You are using %I which as you said is 12-hour format, yet you use 24-hour format in your string, ie 00:47.
00:47 AM is not a valid time specification in any format.
Changing 00:47 AM to 12:47 AM, or %I to %H, fixes this issue.
This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 6 years ago.
I am using an API, and the API needs this data format:
Wed Jan 07 2015 18:58:40
How I can convert the time now to this data format, using the datetime and time modules?
print(datetime.now().strftime('%a %b %d %Y %H:%M:%S'))
Would display something like:
Thu Mar 24 2016 10:09:18
The formatting options used are as follows:
%a Weekday as locale’s abbreviated name.
%b Month as locale’s abbreviated name.
%d Day of the month as a zero-padded decimal number.
%Y Year with century as a decimal number.
%H Hour (24-hour clock) as a zero-padded decimal number.
%M Minute as a zero-padded decimal number.
%S Second as a zero-padded decimal number.
To then convert this to a format for sending, you probably want to investigate quote_plus(), for example:
from datetime import datetime
import urllib
now = datetime.now().strftime('%a %b %d %Y %H:%M:%S')
print(urllib.parse.quote_plus(now))
This would give you:
Thu+Mar+24+2016+10%3A32%3A51