LoginSignup
2
2

More than 5 years have passed since last update.

mongodbでapacheのアクセスログをISODateに変換する関数

Posted at

apacheのリクエスト時間のデフォルトの日付フォーマットをISODateに渡せるように変換スクリプトを書いてみた

apacheDate2ISODate.js
    apacheDate2ISODate = function (apacheDate)
    {

        rawStr = apacheDate.replace(/^(\[)/, "").replace(/\]/, "")
        dateArr = rawStr.split(" ");

        dateSeparate  = dateArr[0].split('/')
        dateSeparate2 = dateSeparate[2].split(':')

        day   = dateSeparate[0]
        month = dateSeparate[1]
        year  = dateSeparate2[0]
        hours = dateSeparate2[1]
        minute = dateSeparate2[2]
        second = dateSeparate2[3]
        date   = dateSeparate2[1] + dateSeparate2[2] + dateSeparate2[3]

        DateString = month + " " + day + " " + year + " " + hours + ":" + minute + ":" + second
        convertDate = new Date(DateString)

        date     = ( convertDate.getDate()   < 10 )  ? '0' + convertDate.getDate()    : convertDate.getDate()
        month    = ( convertDate.getMonth()   < 10 ) ? '0' + convertDate.getMonth()   : convertDate.getMonth()
        hours    = ( convertDate.getHours()   < 10 ) ? '0' + convertDate.getHours()   : convertDate.getHours()
        minute   = ( convertDate.getMinutes() < 10 ) ? '0' + convertDate.getMinutes() : convertDate.getMinutes()
        second   = ( convertDate.getSeconds() < 10 ) ? '0' + convertDate.getSeconds() : convertDate.getSeconds()

        dateStr = convertDate.getFullYear() + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second

        return ISODate(dateStr)
    }

使い方

apacheDate2ISODate(apacheのリクエスト時間)

多分合ってると思う。。。

2
2
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
2
2