0

I am working with a CSV file and I am trying to do something equivalent to calling new Date() and pass in some argument to customize what day month and year this argument points to like so:

new Date(2020, 0, 15);

"2020-01-15T06:00:00.000Z"

Initially, my CSV file looked like this:

'Urban Residential and Combined Industrial/Commercial',
[start:run]     '1/19/15',
[start:run]     '12/8/15',
[start:run]     '1',
[start:run]     'CIC(PD)',
[start:run]     '1/19/15',

but then I converted it in Numbers so that it shows the full year and so my code below should work in the following manner:

export const dateStringToDate = (dateString: string): Date => {
  // 12/18/2014
  const dateParts = dateString.split("/").map((value: string): number => {
    return parseInt(value);
  }); // ['12', '18', '2014']

  return new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
};

and then importing into my CsvFileReader:

import fs from "fs";
import { dateStringToDate } from "./utils";

export class CsvFileReader {
  data: string[][] = [];

  constructor(public filename: string) {}

  read(): void {
    this.data = fs
      .readFileSync(this.filename, {
        encoding: "utf-8",
      })
      .split("\n")
      .map((row: string): string[] => {
        return row.split(",");
      })
      .map((row: string[]): any => {
        return [dateStringToDate(row[0])];
      });
  }
}

I am still getting Invalid Dates.

    [
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   [ Invalid Date ], [ Invalid Date ], [ Invalid Date ], [ Invalid Date ],
[start:run]   ... 4348 more items
[start:run] ]
Daniel
  • 14,004
  • 16
  • 96
  • 156
  • 1
    "*But when I apply it to my CSV file I get the following error:*" which is? – VLAZ May 06 '21 at 20:42
  • 2
    `return new Date();` you never pass any arguments? When you call `new Date()` without arguments, you get the current date-time. – VLAZ May 06 '21 at 20:43
  • @VLAZ, yep, sorry let me fix this OP. – Daniel May 06 '21 at 20:46
  • OK, so you radically changed your question. So, can you tell me why you think year 14 (fun historic fact - the year the Augustus died - first emperor of Rome) had 18 months? Because apparently that's the date you're trying to construct based on the comment in your code. – VLAZ May 06 '21 at 20:55
  • @VLAZ Looks like a mistake with American date format from OP – Cjmarkham May 06 '21 at 21:05
  • @CarlMarkham I still don't think year 14 is correct. – VLAZ May 06 '21 at 21:14
  • @VLAZ, that is how it reads in the CSV. If you take a look above the code snippet where you see 14, notice those dates, thats how they are all printing out, including 12/18/14 and 14, along with 15 and others is assumed to be 2014. I did not put the data together, I am simply trying to work with it. Any suggestions such as, add 20 in the Excel file and then convert it to CSV again or whatever would be appreciated. – Daniel May 06 '21 at 23:32
  • @VLAZ, as I read your criticism, you are on to something, the year should probably be a four digit number and not two digit. So my question then would be, what is the most efficient way to go into thousands of line of dates and change the two digit 14 to 2014 and so on. – Daniel May 06 '21 at 23:34
  • @VLAZ, unfortunately, the lack of a 20 on the year made no difference. – Daniel May 07 '21 at 00:58
  • @CarlMarkham, I moved these around `return new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);` like three card monty and I still got Invalid Dates. So I don't think its an issue with American or European date format. – Daniel May 07 '21 at 01:00
  • [Seems to work](https://jsbin.com/tomaguq/1/edit?js,console) as long as you don't use `'18' - 1` for your month – VLAZ May 07 '21 at 09:12
  • @VLAZ, thank you, but this is the second place that causing it to give invalid date `.map((row: string[]): any => { return [dateStringToDate(row[0])]; });` – Daniel May 07 '21 at 15:31

1 Answers1

0

I think that you must work with day.js, becouse it's a framework that makes easier work with dates.

with a simple function you can transform a string into a date:

dayjs("12-25-1995", "MM-DD-YYYY")

with this function you can transform to any format and any languages, you have the documentation here: https://day.js.org/docs/en/installation/installation

an alternative to day.js is moment.js but is deprecated