Python Forum
Date Format Changing Program Not Working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date Format Changing Program Not Working
#1
So below is the program that I am using to change files with European (DD-MM-YYYY) style date to American (MM-DD-YYYY) Style Date:


Quote:#! python3
   # renameDates.py - Renames filenames with American MM-DD-YYYY date format
   # to European DD-MM-YYYY.

import shutil, os, re

# Create a regex that matches files with the American date format.
datePattern = re.compile(r"""^(.*?) # all text before the date
    ((0|1)?\d)-                     # one or two digits for the month
    ((0|1|2|3)?\d)-                 # one or two digits for the day
    ((19|20)\d\d)                   # four digits for the year
    (.*?)$                          # all text after the date
    """, re.VERBOSE)

# Loop over the files in the working directory.
for amerFilename in os.listdir('.'):
    mo = datePattern.search(amerFilename)

    # Skip files without a date.
    if mo == None:
        continue

    # Get the different parts of the filename.
    beforePart = mo.group(1)
    monthPart  = mo.group(2)
    dayPart    = mo.group(4)
    yearPart   = mo.group(6)
    afterPart  = mo.group(8)
    
    # Form the European-style filename.
    euroFilename = beforePart + dayPart + '-' + monthPart + '-' + yearPart + afterPart
    
    # Get the full, absolute file paths.
    absWorkingDir = os.path.abspath('.')
    amerFilename = os.path.join(absWorkingDir, amerFilename)
    euroFilename = os.path.join(absWorkingDir, euroFilename)
    
    # Rename the files.
    print('Renaming "%s" to "%s"...' % (amerFilename, euroFilename))
    shutil.move(amerFilename, euroFilename) 


However, the files whose days that have 20-29 in their dates does not work properly.
For example, ab12-10-2010test.txt works but ab22-10-2010test.txt does not work.
Is there some problem with the regular expression.
Reply
#2
Quote:So below is the program that I am using to change files with European (DD-MM-YYYY) style date to American (MM-DD-YYYY) 

you could just use datetime to convert the date. 

from datetime import datetime

time = '13-01-2017'

def convert(t):
    date_format = "%d-%m-%Y"
    convert_to_format = "%m-%d-%Y"
    return datetime.strptime(t, date_format).date().strftime(convert_to_format)

print(convert(time))
Output:
01-13-2017
Quote:For example, ab12-10-2010test.txt works but ab22-10-2010test.txt does not work.
then just extract the date portion of the string, convert it, then reinsert the old portion wrapped around the new date format and rename it. The input could be anything even 2-1-2017 and it will still output 01-02-2017
Recommended Tutorials:
Reply
#3
(Jan-28-2017, 05:25 PM)pyth0nus3r Wrote: So below is the program that I am using to change files with European (DD-MM-YYYY) style date to American (MM-DD-YYYY) Style Date:

Quote:#! python3
   # renameDates.py - Renames filenames with American MM-DD-YYYY date format
   # to European DD-MM-YYYY.

Not very clear... changing European to American or vice-versa? Your regexp implies that your input is American (month first).

Several remarks:

  1. (0|1|2|3) is best replaced by [0123] or even [0-3]
  2. you can use re.sub (and back-references) to replace just the part that matches so you regexp doesn't have to deal with the bits outside the date part:
datePattern = re.compile(r'([01]?\d)-([0-3]?\d)-((19|20)\d\d)')

nameOut=re.sub(datePattern,r'\2-\1-\3',nameIn)
If nameOut==nameIn, the regexp was unmatched... or you where dealing with a file dated Jan 1st, Feb 2nd, etc... In any case you don't need to rename the file.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 243 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 613 Jan-20-2024, 04:45 AM
Last Post: 1418
  Modifying a date format jehoshua 17 3,006 Oct-29-2022, 08:44 PM
Last Post: jehoshua
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,669 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  Date format error getting weekday value Aggie64 2 1,427 May-29-2022, 07:04 PM
Last Post: Aggie64
  Convert Date to another format lonesoac0 2 1,678 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 996 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 1,532 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 4,271 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 2,027 Sep-29-2021, 06:43 AM
Last Post: MyerzzD

Forum Jump:

User Panel Messages

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