Python Forum
How to convert dates in odd format to months
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert dates in odd format to months
#2
Maybe something like this? It assumes a month is exactly 30 days. Not correct, but pretty close....

sample = """0d
6d
7d
1w, 3d
7w, 5d
4m
5m
"""

period = {
        "d": 1,
        "w": 7,
        "m": 30,
        }

for line in sample.splitlines():
    total_time = 0
    for time in line.split(", "):
        qty, unit = time[:-1], time[-1]
        qty = int(qty)
        total_time += qty * period[unit]
        months = total_time // 30
    print(f"{line} => time in days: {total_time}, time in full months: {months}")
Output:
0d => time in days: 0, time in full months: 0 6d => time in days: 6, time in full months: 0 7d => time in days: 7, time in full months: 0 1w, 3d => time in days: 10, time in full months: 0 7w, 5d => time in days: 54, time in full months: 1 4m => time in days: 120, time in full months: 4 5m => time in days: 150, time in full months: 5
Reply


Messages In This Thread
RE: How to convert dates in odd format to months - by bowlofred - Apr-17-2021, 02:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 704 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Convert Json to table format python_student 2 5,062 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert .xlsx to Format as Table bnadir55 0 860 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  how to convert and format a varable darktitan 4 1,622 May-29-2022, 12:59 PM
Last Post: darktitan
  Convert Date to another format lonesoac0 2 1,631 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Illegal instruction? working code for months? korenron 4 12,769 Aug-05-2021, 09:57 AM
Last Post: korenron
  Convert timedelta to specified format Planetary_Assault_Systems 3 3,084 Jun-27-2021, 01:46 PM
Last Post: snippsat
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,161 Mar-11-2021, 10:52 AM
Last Post: buran
  Convert email addresses to VCF format jehoshua 2 4,606 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  Convert date integers (ex. 43831) to regular format Galven 2 2,552 Nov-15-2020, 11:38 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020