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

naive-date

v0.3.0

Published

Like Date, but not a timestamp. Useful for time zone conversions.

Downloads

5

Readme

naive-date

Use a NaiveDate as opposed to Date when you want a Date like object, but one that's not a timestamp (read Javascript's Date is just a timestamp for more details). For example,

  1. You want a YMD date and a time, but these are not linked to any time zone
  2. You want to perform timezone conversions i.e. given a timestamp, what is the local time in Asia/Kolkata v/s America/New_York?
  3. You want to perform calendrical calculations without worrying about the impact of DST transitions (e.g. would adding 86400 seconds always add one whole day?)

NaiveDate's API is very similar to that of Date and includes all of its warts, like month indexes starting from 0. Use NaiveDate when you want to work with a Date like object and don't want to bring in a full fledged date/time handling library in your code.

The term naive is inspired by its usage in the Python datetime module, which categorizes date and time objects as "aware" or "naive" depending on whether they include time zone information or not.

Usage

Installation

Install using npm:

npm install naive-date

Or include it directly in your HTML page

<script src="https://unpkg.com/[email protected]/dist/naive-date.js"></script>

Creation

To create a NaiveDate, you can use the constructor.

import NaiveDate from 'naive-date'

// date only
// since we use 0 based indexes, the month below is Feb, not Jan
const x = new NaiveDate(2022, 1, 1)

// date and time
const y = new NaiveDate(2022, 1, 1, 10, 0, 0)

Since a NaiveDate is not linked to any time zone (and it's not a timestamp), when you print it you won't see any zone info:

x.toString()
// => '2022-02-01T00:00:00.000'

y.toString()
// => '2022-02-01T10:00:00.000'

Getters and Setters

Just like Date, NaiveDate exposes the following getters and setters:

import NaiveDate from 'naive-date'

const x = new NaiveDate(2022, 1, 1, 10, 0, 0)

x.getFullYear()     // 2022
x.getMonth()        // 1
x.getDate()         // 1
x.getHours()        // 10
x.getMinutes()      // 0
x.getSeconds()      // 0
x.getMilliseconds() // 0
x.getDay()          // 2

x.setFullYear()
x.setMonth()
x.setDate()
x.setHours()
x.setMinutes()
x.setSeconds()
x.setMilliseconds()

There's no equivalent for getUTC... and setUTC... methods since they don't make sense (NaiveDate is not a timestamp).

There's no equivalent for getTimezoneOffset() either, since a NaiveDate, by definition, is not linked to any time zone.

Timezone converstions

Given a timestamp (i.e. a Date), use the static method from() to get the local time in a given time zone.

import NaiveDate from 'naive-date'

const x = new Date(Date.UTC(2022, 1, 1, 10, 0, 0))
// => 2022-02-01T10:00:00.000Z

const y = NaiveDate.from(x, 'Asia/Kolkata')
y.toString()
// => '2022-02-01T15:30:00.000'

const z = NaiveDate.from(x, 'America/New_York')
z.toString()
// => '2022-02-01T05:00:00.000'

And to do the opposite i.e. to get a timestamp given a NaiveDate and a timezone, use the instance method .toDate(). This method returns a Date.

import NaiveDate from 'naive-date'

const x = new NaiveDate(2022, 1, 1, 10, 0, 0)
x.toString()
// => '2022-02-01T10:00:00.000'

x.toDate('Asia/Kolkata')
// => 2022-02-01T04:30:00.000Z

x.toDate('America/New_York')
// => 2022-02-01T15:00:00.000Z

Tests

To run tests on the command line using node.js, run npm test.

To run them in the browser, open mocha.html.