npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-stringify-date

v1.0.13

Published

Like JSON.stringify, but preserve timezones in date objects and parse dates into Date object

Downloads

685

Readme

Build Status npm NpmLicense npm

json-stringify-date

Like JSON.stringify, but preserve timezones in date objects and parse dates into Date object.

Install

npm install json-stringify-date

Usage

Takes the same arguments as JSON.stringify and JSON.parse.

var stringifyDate = require('json-stringify-date');
var obj = {d: new Date(2014, 02, 4)};
console.log(stringifyDate.stringify(obj, null, 2));
var text = '{"d": "2014-03-04T00:00:00.000-03:00"}';
console.log(stringifyDate.parse(text));

Output:

{
  "d": "2014-03-04T00:00:00.000-03:00"
}
{ d: Tue Mar 04 2014 00:00:00 GMT-0300 (BRT) }

Methods

stringify

stringifyDate.stringify(value [, replacer [, space]])

Executes exactly the JSON.stringify, but can preserve time zones in dates.

parse

stringifyDate.parse(text [, reviver])

Returns the object containing dates.

getReviver

stringifyDate.getReviver([reviver])

Gets the function passed to JSON.parse, has the ability to pass an inner function through optional parameter reviver.

getReplacer

stringifyDate.getReplacer([replacer])

Gets the function passed to JSON.stringify, has the ability to pass an inner function through optional parameter replacer.

getOptions

stringifyDate.getOptions()

Gets the options current set.

setOptions

stringifyDate.setOptions({...})

Sets the options that will be used.

utc

type: boolean default: false
Format date in utc format

var stringifyDate = require('json-stringify-date');
var obj = {d: new Date(2014, 02, 4)};
stringifyDate.setOptions({utc: true});
console.log(stringifyDate.stringify(obj));
stringifyDate.setOptions({utc: false}); //this is the default
console.log(stringifyDate.stringify(obj));

Output:

{"d": "2014-03-04T00:00:00.000Z"}
{"d": "2014-03-04T00:00:00.000-03:00"}

fnCheck

type: function (string, string) returns: boolean Function to check whenever a string is a valid date

var stringifyDate = require('json-stringify-date');
fallbackFnCheck = stringifyDate.getOptions().fnCheck;
stringifyDate.setOptions({ fnCheck: function (key, value) {
  if (key == 'not-a-date-key') {
    return value;
  }
  return fallbackFnCheck(key, value);
} });
console.log(stringifyDate.parse({'not-a-date-key': '2020-01-01'}));
console.log(stringifyDate.parse({'d': '20200101'}));

Output:

{ 'not-a-date-key': '2020-01-01' } // string
{ d: 2020-01-01T00:00:00.000Z } // [object Date]

Using with ExpressJS

To use it with ExpressJS, follow this example.
The magic really happens in passing getReviver([reviver]) to body-parser 'reviver option', it makes the json parser to serialize date strings into date objects.
Also, optionally you can pass getReplacer([replacer]) to body-parser 'json replacer setting', it makes the resulting json to preserve timezones.

var express = require('express');
var bodyParser = require('body-parser');
var jsonStringifyDate = require('json-stringify-date');
var app = express();
app.use(bodyParser.json({reviver: jsonStringifyDate.getReviver()}));
app.set('json replacer', jsonStringifyDate.getReplacer());
app.post('/test', function (req, res) {
  req.body.somedate // do something
  res.json({date: new Date()});
});
app.listen(3000);

Using with Browser

Add tag <script type="text/javascript" src="https://raw.githubusercontent.com/fmenezes/json-stringify-date/master/browser.js"></script>, you can also use webpack or any other packaging system.

Than you will have the object JSONStringifyDate in the global context (window) so you can run things like

JSONStringifyDate.parse('{"d": "2014-03-04T00:00:00.000-03:00"}');

Output:

{ d: Tue Mar 04 2014 00:00:00 GMT-0300 (BRT) }

Legal

See LICENSE