datetime_functions.py 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import datetime
  2. from calendar import monthrange, month_name
  3. def datetime_to_str(datetime_obj, format):
  4. '''
  5. :param format:
  6. %V = Calendar Week
  7. %d/%m%Y
  8. :return:
  9. '''
  10. if datetime_obj == 'current':
  11. datetime_obj = datetime.datetime.now()
  12. return datetime_obj.strftime(format)
  13. else:
  14. return datetime_obj.strftime(format)
  15. def get_current_date():
  16. return datetime.datetime.now()
  17. def get_current_month_number():
  18. current_date = datetime.datetime.now()
  19. return int(current_date.strftime("%m"))
  20. def add_days_to_date(datetime_obj,count):
  21. return datetime_obj + datetime.timedelta(days= count)
  22. def substract_days_to_current_date(count):
  23. return datetime.datetime.now() - datetime.timedelta(days= count)
  24. def get_count_of_days_for_specific_month(year, month_number):
  25. '''
  26. :param year: As Int, ex. 2012
  27. :param month_number: As Int, ex. February == 2
  28. :return:
  29. '''
  30. return monthrange(year,month_number)[1] # [0] contains the week day