Convert query strings to dates for express/connect applications.
npm install --save express-query-date
The module will recursively attempt to parse every property in req.query
.
Load it right after bodyParser
:
var dateParser = require('express-query-date');
// [...]
app.use(bodyParser.json());
app.use(dateParser());
// ?a=2015-01-01T05:00:00.000Z&b[c]=2015-01-01T05:00:00.000Z
console.log(req.query);
// => { a: '2015-01-01T05:00:00.000Z', b: { c: '2015-01-01T05:00:00.000Z' } }
// ?a=2015-01-01T05:00:00.000Z&b[c]=2015-01-01T05:00:00.000Z
console.log(req.query);
// => { a: Date 2015-01-01T05:00:00.000Z, b: { c: Date 2015-01-01T05:00:00.000Z } }
Default:
['x', moment.ISO_8601]
To use your own, provide them when initializing the module:
app.use(dateParser({
formats: ['MM-DD-YYYY']
}));
See moment.js documentation for more.
Strict format matching is on by default. To disable this, set options.strict
to false when initializing the module.
app.use(dateParser({
strict: false
}));
Copyright (c) 2015 Marius Craciunoiu. Licensed under the MIT license.