Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ The current date and time in UTC. The available formats are:

- `clf` for the common log format (`"10/Oct/2000:13:55:36 +0000"`)
- `iso` for the common ISO 8601 date time format (`2000-10-10T13:55:36.000Z`)
- `locale` for the locale time zone format (`2016-03-09, 21:55:36`)
- `web` for the common RFC 1123 date time format (`Tue, 10 Oct 2000 13:55:36 GMT`)

If no format is given, then the default is `web`.
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ morgan.token('date', function getDateToken(req, res, format) {
return clfdate(date)
case 'iso':
return date.toISOString()
case 'locale':
return localedate(date)
case 'web':
return date.toUTCString()
}
Expand Down Expand Up @@ -362,6 +364,18 @@ function clfdate(dateTime) {
+ ' +0000'
}

function localedate(dateTime) {
var date = dateTime.getDate()
var hour = dateTime.getHours()
var mins = dateTime.getMinutes()
var secs = dateTime.getSeconds()
var year = dateTime.getFullYear()
var month = dateTime.getMonth() + 1;

return year + '-' + pad2(month) + '-' + pad2(date)
+ ' ' + pad2(hour) + ':' + pad2(mins) + ':' + pad2(secs)
}

/**
* Compile a format string into a function.
*
Expand Down
16 changes: 16 additions & 0 deletions test/morgan.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ describe('morgan()', function () {
.expect(200, cb)
})

it('should get current date in "locale" format', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
assert.ok(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(line))
done()
})

var stream = createLineStream(function (line) {
cb(null, null, line)
})

request(createServer(':date[locale]', { stream: stream }))
.get('/')
.expect(200, cb)
})

it('should get current date in "web" format', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
Expand Down