0

I am trying to parse some csv data, however, I am not able to parse date with momentjs library.

var csv = require('csv-parser');
var fs = require('fs');
var moment = require('moment');

const bittrexDateFormat = "MM/DD/YYYY hh:mm:ss a";

var count = 0;
fs.createReadStream('orders.csv')
    .pipe(csv({
        headers: ['OrderUuid', 'Exchange', 'Type', 'Quantity', 'Limit', 'CommissionPaid', 'Price', 'Opened', 'Closed']
    }))
    .on('data', function(data) {
        var createDate = moment(data.Opened, bittrexDateFormat);
        console.log(createDate.toDate());
    });

And the csv data looks like;

OrderUuid,Exchange,Type,Quantity,Limit,CommissionPaid,Price,Opened,Closed
24245deb-134c-4da7-990e-8d22d8fd728c,BTC-STRAT,LIMIT_SELL,77.12739479,0.00087503,0.00016874,0.06749952,12/24/2017 12:09:20 AM,12/24/2017 12:09:21 AM

And this the output;

0002-01-02T09:00:02.000Z

On the other hand, if I directly hardcode the date string I am able to get Date object.

var createDate = moment("12/24/2017 12:09:20 AM", bittrexDateFormat);
console.log(createDate.toDate());

Another thing I figured out is if I print data in event .on('data') it prints encoded string version

Row {
  OrderUuid: 'O\u0000r\u0000d\u0000e\u0000r\u0000U\u0000u\u0000i\u0000d\u0000',
  Exchange: '\u0000E\u0000x\u0000c\u0000h\u0000a\u0000n\u0000g\u0000e\u0000',
  Type: '\u0000T\u0000y\u0000p\u0000e\u0000',
  Quantity: '\u0000Q\u0000u\u0000a\u0000n\u0000t\u0000i\u0000t\u0000y\u0000',
  Limit: '\u0000L\u0000i\u0000m\u0000i\u0000t\u0000',
  CommissionPaid: '\u0000C\u0000o\u0000m\u0000m\u0000i\u0000s\u0000s\u0000i\u0000o\u0000n\u0000P\u0000a\u0000i\u0000d\u0000',
  Price: '\u0000P\u0000r\u0000i\u0000c\u0000e\u0000',
  Opened: '\u0000O\u0000p\u0000e\u0000n\u0000e\u0000d\u0000',
  Closed: '\u0000C\u0000l\u0000o\u0000s\u0000e\u0000d\u0000\r\u0000' }
Row {
  OrderUuid: '\u00002\u00004\u00002\u00004\u00005\u0000d\u0000e\u0000b\u0000-\u00001\u00003\u00004\u0000c\u0000-\u00004\u0000d\u0000a\u00007\u0000-\u00009\u00009\u00000\u0000e\u0000-\u00008\u0000d\u00002\u00002\u0000d\u00008\u0000f\u0000d\u00007\u00002\u00008\u0000c\u0000',
  Exchange: '\u0000B\u0000T\u0000C\u0000-\u0000S\u0000T\u0000R\u0000A\u0000T\u0000',
  Type: '\u0000L\u0000I\u0000M\u0000I\u0000T\u0000_\u0000S\u0000E\u0000L\u0000L\u0000',
  Quantity: '\u00007\u00007\u0000.\u00001\u00002\u00007\u00003\u00009\u00004\u00007\u00009\u0000',
  Limit: '\u00000\u0000.\u00000\u00000\u00000\u00008\u00007\u00005\u00000\u00003\u0000',
  CommissionPaid: '\u00000\u0000.\u00000\u00000\u00000\u00001\u00006\u00008\u00007\u00004\u0000',
  Price: '\u00000\u0000.\u00000\u00006\u00007\u00004\u00009\u00009\u00005\u00002\u0000',
  Opened: '\u00001\u00002\u0000/\u00002\u00004\u0000/\u00002\u00000\u00001\u00007\u0000 \u00001\u00002\u0000:\u00000\u00009\u0000:\u00002\u00000\u0000 \u0000A\u0000M\u0000',
  Closed: '\u00001\u00002\u0000/\u00002\u00004\u0000/\u00002\u00000\u00001\u00007\u0000 \u00001\u00002\u0000:\u00000\u00009\u0000:\u00002\u00001\u0000 \u0000A\u0000M\u0000' }

I am pretty new to nodejs but I don't think the problem occurs from either momentjs or csv-parser libraries. Instead it should be string format of stream api nodejs. Thanks a lot.

quartaela
  • 2,579
  • 16
  • 63
  • 99
  • what is the expected output? – George Mar 12 '18 at 05:49
  • @GeorgeCampbell The expected output is `2017-12-24T08:09:20.000Z`. I will convert it to UTC timezone later. – quartaela Mar 12 '18 at 06:00
  • log the value of data.Opened and see what is the o/p – krishank Tripathi Mar 12 '18 at 06:45
  • if you are getting format likely this one 0002-01-02T09:00:02.000Z then your date and bittrexDateFormat is not matching – krishank Tripathi Mar 12 '18 at 06:46
  • beside 0002-01-02T09:00:02.000Z date is wrong because first four digit in this will determine your year. – krishank Tripathi Mar 12 '18 at 06:49
  • @krishankTripathi the weird part is I am able to parse date outside createReadStream block. you can also try and verify to get date with "12/24/2017 12:09:20 AM" and format "MM/DD/YYYY hh:mm:ss a". however in `.on("data")` action it doesn't work. that is why I think there is encoding issue in createReadStream that I am not able to parse it – quartaela Mar 12 '18 at 07:18
  • @krishankTripathi console.log(data.Opened) gives the correct output which is "12/24/2017 12:09:20 AM" but I am not able to parse the date object by passing `data.Opened` into moment class. – quartaela Mar 12 '18 at 07:20

1 Answers1

1

I just ran your code, and it worked just fine. Try updating your node version or try invoking the function with

fs.createReadStream('orders.csv', 'utf16le')

the encoding should make the difference... https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

rex
  • 421
  • 4
  • 12