LoginSignup
0
0

More than 5 years have passed since last update.

csv-stringifyを使用するとdatetimeがエポック秒(UNIX時間)になる

Posted at

csv-stringifyを使うとエポック秒(UNIX時間)になる

背景

lambdaがサポートしているnodeがv6.10のためそれを使用
csv形式で共有したい
sequelizeを使用していたためcreatedAt,updatedAtカラムがあり両方ともdate型
それ以外のカラムは日付情報を持っていない

csv-stringifyの基本的な書き方

const stringify = require('csv-stringify')
stringify([data], [options], [callback])

//例
stringify(data, (err, output)=>{
  console.log(output)
}

エポック秒(UNIX時間)からYYYY/MM/DD形式への変換

require('date-utils');
const stringify = require('csv-stringify')

stringify(
    data,
    {
        formatters: {
            date: function(value) {
               return value.toFormat("YYYY/MM/DD")
            }
        }
    },
    (err, output)=>{
         console.log(output)
    }
);

注意点

returnのあとを変更すればmomentも使うことができる
今回はdate-utilsを使用してくれという指示だったので使用
option:formattersのオプションdateを使いvalueのformatを指定していく
dateはカラム名ではない
date型全て変換してしまうためこの方法では個別指定はできない

個別指定をしたい場合

option:columnを使用してoutputをゴニョゴニョするしかない

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0