I am trying to get the current date in ISO Calendar format as follows alongwith the zero padding on the week?
2019/W06
I tried the following, but prefer something using strftime as it is much easier to read.
print(str(datetime.datetime.today().isocalendar()[0]) + '/W' + str(datetime.datetime.today().isocalendar()[1]))
2019/W6
Use following code:
print(datetime.now().strftime('%Y/W%V'))
%Y Year with century as a decimal number.
%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.
https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior
Solution with strftime:
If you want the zero padding:
datetime.date.today().strftime("%Y/W%V")
Output:
2019/W06
If you don't want it:
datetime.date.today().strftime("%Y/W%-V")
Output:
2019/W6
Note that "%V" returns the week number, and the "-" is what removes the leading zero.
Related
n=10
I want to get the week commencing date of the 10th week of the current year (2022), i.e., 3/7/2022. How can this be done using datetime functions?
You need to use the %W directive, but you also need to specify what the start day is of the week and of course the year.
Example:
from datetime import datetime
print(datetime.strptime("10-2022-1", "%W-%Y-%w"))
Result:
2022-03-07 00:00:00
10 is the week number
2022 is the year
1 is the day of the week to start with (Monday) in order to get an actual date.
Datetime formats: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
Today one of my script gave an error for an invalid datetime format as an input. The script is expecting the datetime input as '%m/%d/%Y', but it got it in an entirely different format. For example, the date should have been 5/2/2022 but it was May 2, 2022. To add a bit more information for clarity, the input is coming for a Google sheet and the entire date is in a single cell (rather than different cells for month, date and year).
Is there a way to convert this kind of worded format to the desired datetime format before the script starts any kind of processing?
If you're in presence of the full month name, try this:
>>> pd.to_datetime(df["Date"], format="%B %d, %Y")
0 2022-05-02
Name: Date, dtype: datetime64[ns]
According to the Python docs:
%B: "Month as locale’s full name".
%d: "Day of the month as a zero-padded decimal number". (Although it seems to work in this case)
%Y: "Year with century as a decimal number."
Now, if you want to transform this date to the format you initially expected, just transform the series using .dt.strftime:
>>> pd.to_datetime(df["Date"], format="%B %d, %Y").dt.strftime("%m/%d/%Y")
0 05/02/2022
Name: Date, dtype: object
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
I expected datetime.strftime and datetime.strptime calls to be reversible. Such that calling
datetime.strptime(datetime.now().strftime(fmt), fmt))
Would give the closest reconstruction of now() (given the information preserved by the format).
However, this is not the case when formatting a date to a string with a YYYY-Week# format:
>>> yyyy_u = datetime.datetime(1992, 5, 17).strftime('%Y-%U')
>>> print(yyyy_u)
'1992-20'
Formatting the string back to a date does not give the expected response:
>>> datetime.datetime.strptime(yyyy_u, '%Y-%U')
datetime.datetime(1992, 1, 1, 0, 0)
I would have expected the response to be the first day of week 20 in 1992 (17 May 1992).
Is this a failure of the %U format option or more generally are datetime.strftime and datetime.strptime calls not meant to be reversible?
From the Python docs regarding strptime() behaviour:
When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the year are specified.
Day of the week must be specified along with Week number and Year.
(%Y-%U-%w)
datetime.datetime.strptime('1992-20-0', '%Y-%U-%w') gives the first day of week 20 for 1992 year.
I need to parse a few dates that are roughly in the format (1 or 2-digit year)-(Month abbreviation), for example:
5-Jun (June 2005)
13-Jan (January 2013)
I tried using strptime with the format %b-%y but it did not consistently produce the desired date. Per the documentation, this is because some years in my dataset are not zero-padded.
Further, when I tested the datetime module (please see below for my code) on the string "5-Jun", I got "2019-06-05", instead of the desired result (June 2005), even if I set yearfirst=True when calling parse.
from dateutil.parser import parse
parsed = parse("5-Jun",yearfirst=True)
print(parsed)
It will be easier if 0 is padded to single digit years, as it can be directly converted to time using format. Regular expression is used here to replace any instance of single digit number with it's '0 padded in front' value. I've used regex from here.
Sample code:
import re
match_condn = r'\b([0-9])\b'
replace_str = r'0\1'
datetime.strptime(re.sub(match_condn, replace_str, '15-Jun'), '%y-%b').strftime("%B %Y")
Output:
June 2015
One approach is to use str.zfill
Ex:
import datetime
d = ["5-Jun", "13-Jan"]
for date in d:
date, month = date.split("-")
date = date.zfill(2)
print(datetime.datetime.strptime(date+"-"+month, "%y-%b").strftime("%B %Y"))
Output:
June 2005
January 2013
Ah. I see from #Rakesh's answer what your data is about. I thought you needed to parse the full name of the month. So you had your two terms %b and %y backwards, but then you had the problem with the single-digit years. I get it now. Here's a much simpler way to get what you want if you can assume your dates are always in one of the two formats you indicate:
inp = "5-Jun"
t = time.strptime(("0" + inp)[-6:], "%y-%b")