SoFunction
Updated on 2024-11-10

python use time, datetime return weekday list example code

Recently in the study of python, hands to do a small tool to automatically fill in the daily newspaper; due to the request contains time, format such as: 2016-08-04; so understand the time and date related functions of python; here to do a simple record.

The function function is very simple: get all the working days of the month (excluding Saturday and Sunday); if the script runs on Saturday or Sunday, add the day.

# Get the date to fill in the daily newspaper, rule: return all the working days of the month all the time
  def getDateByTime(self):
    =[]
    t = str(('%Y-%m-'))
    for i in range(1,32):
      timeStr=t+str(i)
      try:
        Conversion of # strings to time in a defined format
        tmp = (timeStr,'%Y-%m-%d')
        #Determine if it's a Saturday or Sunday
        if (tmp.tm_wday !=6) and (tmp.tm_wday!=5):
          (('%Y-%m-%d',tmp))
      except:
        print('Date transgression')
    if len()==0:
      (('%Y-%m-%d'))
    return 

  def getDateByDateTime(self):
    =[]
    now = ()
    tmp = ('%Y-%m-')
    # Get the weekday of the first day of the month and the number of days in the month via calendar
    t = (, )
    for i in range(1,t[1]):
      dateTmp = tmp+str(i)
      myDateTmp = (dateTmp,'%Y-%m-%d')
      if () !=6 and () !=7:
        (('%Y-%m-%d'))
    if len()==0:
      (('%Y-%m-%d'))
    return 

The above is a small introduction to the python use time, datetime to return to the list of workdays detailed integration, I hope to help you, if you have any questions please leave me a message, I will reply to you in time. Here also thank you very much for your support of my website!