Adding short dates with PapaParse leads to 19xx

Tap Forms – Organizer Database App for Mac, iPhone, and iPad Forums Script Talk Adding short dates with PapaParse leads to 19xx

Viewing 4 reply threads
  • Author
    Posts
  • April 11, 2022 at 11:26 PM #47085

    Chris Ju
    Participant

    Hello,

    since the last Tap Forms update, the import from CSV files with the PapaParse script leads from short dates dd.MM.22 to dd.MM.1922. For example, the German date 12.04.22 becomes 12.04.1922 on import. It should be 12.04.2022. That wasn’t a problem in the last version.

    Does anyone have an idea to solve this issue without splitting the date and adding 100 to the year? This would lead to an error if the year in the date is really 19xx.
    e.g.:

    
    ...
    let datum_string = line[0];
    let datum = parseDate(datum_string);
    let conv_date = new Date(datum.getFullYear() + 100, datum.getMonth(), datum.getDate());
    ...
    

    BTW: Copy&Pasting the short date from the CSV file to the date field works fine.

    Thanks in advance.

    Chris

    • This topic was modified 2 years, 7 months ago by Chris Ju.
    • This topic was modified 2 years, 7 months ago by Chris Ju.
    April 11, 2022 at 11:41 PM #47090

    Daniel Leu
    Participant

    Can you share the script you are using or a test case to reproduce this?

    April 11, 2022 at 11:58 PM #47092

    Brendan
    Keymaster

    So how does one determine the difference between 1922 and 2022 then in your data if you’re only using 2 digits for the year. There’s a reason why the switch to the year 2000 was very troublesome.

    It’s the operating system that would be interpreting the actual date values, not Tap Forms itself. I just pass in the pattern used for dates (e.g. yyyy-MM-dd) and the operating system does the rest to interpret the string representation of the date into an actual Date object that can then be treated as such.

    April 12, 2022 at 12:10 AM #47094

    Chris Ju
    Participant

    I saw when looking through the upper part of the script again that I had already “converted” the date (some months ago with your help… Thanks again for that!). I now changed that part, that “20” is added to the “short year”. However, this leads to errors with dates with years before 2000…

    But why does the problem appear in the new version?

    BTW: Does anyone know, why the “header: true” option doesn’t work?


    // function Importiere_Receipts_Daten() {

    // this imports the Papa Parse script
    // form.runScriptNamed('PapaParse');
    document.getFormNamed('Script Manager').runScriptNamed('PapaParse');

    // Receipts Feld-Ids: Zuordnung...
    // Bezahlt -> Datum
    // Kontakt -> Buchungstext manuell
    // Schlagwörter -> Buchungsart
    // Betrag in EUR -> Umsatz1
    // Einnahmen und Ausgaben -> EA-Kennzeichnung

    var datum_id = 'fld-98e4dff9f7d74050a6580f44bebf4e9a';
    var buchungstext_manuell_id = 'fld-16dda6d19d264f529e57526592e49e36';
    var buchungsart_id = 'fld-e204ad0f420845aa972d8ed527577546';
    var umsatz1_id = 'fld-289dc3baf44941b9beb8cbe14292f43a';
    var ea_kennzeichnung_id = 'fld-2fc88950003549f888369c5a99214597';

    // -------- # User dir, import file
    var user_dir = Utils.getUserName();
    var import_file_name = 'tf_receipts.csv'

    // -------- # Datumskorrekturen mit parseDate (Thx to Sam Moffatt, Brendan & Daniel Leu)
    function parseDate(dateString) {
    if (!dateString) {
    return undefined;
    }
    let pieces = dateString.split('.');
    return new Date("20" + pieces[2], pieces[1] - 1, pieces[0]);
    }

    // ------- # Import
    function Import_Entries() {
    let filename = 'file:////Users/' + user_dir + '/Documents/tapforms_dir/' + import_file_name;
    let csvFile = Utils.getTextFromUrl(filename);
    if (!csvFile) {
    console.log("No input file?");
    return
    }
    var output = Papa.parse(csvFile, {
    delimiter: ";",
    // header: false,
    });
    // # abort if there are any errors and log to console.
    if (output.errors.length > 0) {
    console.log(errors.join("\n"));
    return;
    }
    // # read each line
    for (let line of output.data) {
    // ------------- date correction
    let datum_string = line[0];
    let datum = parseDate(datum_string);
    // let conv_date = new Date(datum.getFullYear() + 100, datum.getMonth(), datum.getDate());
    if ((line[0] !== 'Bezahlt') || (typeof line[0] === typeof undefined)) {
    var newRecord = form.addNewRecord();
    newRecord.setFieldValues({
    [datum_id]: datum,
    [buchungstext_manuell_id]: line[1],
    [buchungsart_id]: line[2],
    [umsatz1_id]: line[3],
    [ea_kennzeichnung_id]: line[4],
    });
    } else {
    }
    document.saveAllChanges();
    }
    }
    Import_Entries();

    • This reply was modified 2 years, 7 months ago by Chris Ju.
    • This reply was modified 2 years, 7 months ago by Chris Ju.
    Attachments:
    You must be logged in to view attached files.
    April 21, 2022 at 10:08 PM #47160

    Chris Ju
    Participant

    BTW: Date format in exported CSV files depends regarding some apps on the system setting, see screenshot…

    In my case the year of the short date was set to two digits. While using the CSV export in many apps everything was fine, because these apps uses the medium date format for export. Only one app uses the short date format, which leads to a two digits year and because of that format to the described issue of this forum entry…

    Maybe that information helps someone to fix some issues…

    Attachments:
    You must be logged in to view attached files.
Viewing 4 reply threads

You must be logged in to reply to this topic.