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

py-datetime

v0.9.1

Published

Datetime handling the python way (or as close as possible)

Downloads

36

Readme

Python-style datetime handling lib for ES6

This tiny lib emulates most of the main functions of Python's datetime lib to simplify datetime handling in JavaScript that is notoriously cumbersome in this regard. It tries to stay as close to the python API as possible - ideal if you are working with both Python and JS projects. Hope you'll find it useful! Live console here: https://npm.runkit.com/py-datetime

Install

npm install py-datetime

Demo

import {default as dt} from "py-datetime";  // const dt = require("py-datetime"); for node imports

let now = dt.datetime.now()
let today = dt.datetime.combine(now, dt.time())
console.log("Now:", now.strftime("%Y-%m-%d %H:%M:%S"));
console.log("Today:", today.strftime("%Y-%m-%d"));
console.log("Minutes since midnight:", dt.timedelta(now - today).totalSeconds() / 60);

A few gotchas

  • dt.timedelta(days, [seconds, [milliseconds..) constructor can't really guess whether you are passing in a day or a result from date math (as dt - dt in javascript will return an int), so i've arbitrarily put in a condition where if it's under 900 we treat it as days, but otherwise it's millis (1000 millis = 1 sec). For most cases this should work just fine, but where disambiguation is required, you can be be explicit about it: dt.timedelta({days: ..}) and `dt.timedelta({millisesconds: ..}), respectively.
  • use .str() to get string representation of the object. JavaScript's toString() will return Unix epoch.

List of supported functions

Note: all objects evaluate to milliseconds, meaning dt.datetime.now() + 0 will give you time in Unix epoch. you can also call <object>.toString directly.

dt.datetime

  • dt.datetime(year, month, day, hour, minute, second, millisecond) - gets you a brand new datetime
  • dt.datetime(jsDate) - you can also pass in a JavaScript Date object
  • dt.datetime(unixEpoch) - this will also work
  • dt.datetime(datetime) - this will clone an existing datetime object)
  • dt.datetime.strptime(dateString, format, utc) - parse given date string into a new datetime. Format uses posix parsing see d3-time-format for details. The third param is an optional boolean. When set to true, dateString will be assumed to be in UTC.
  • dt.datetime.now() - return current time
  • dt.datetime.utcnow() - return current time in UTC
  • dt.datetime.combine(date, time) - combines passed in dt.date or dt.datetime with the passed in dt.time to create a new datetime
  • datetime.replace(year, month, day, hour, minute, second, millisecond) returns a new datetime with items replaced as requested
  • datetime.jsDate property returns JavaScript Date object representing the datetime
  • datetime.str() returns analog of python's str(datetime) which is %Y-%m-%d %H:%M:%S.%f

dt.date

  • dt.date(year, month, day) - creates a, well, timeless object, all three params are mandatory
  • date.jsDate property returns JavaScript Date object representing the datetime
  • datetime.str() returns analog of python's str(date), which is %Y-%m-%d

dt.time

  • dt.time(hour, minute, second, millisecond) - return a new time object
  • dt.time(time) - clone an existing dt.time object
  • time.str() returns analog of python's str(time), which is %H:%M:%S.%f

dt.timedelta

  • dt.timedelta(days, seconds, milliseconds, minutes, hours, weeks) - return a new time object. the param order is not random and matches python.
  • dt.timedelta(millis) - this will work if millis is > 900. will initiate the timedelta object from milliseconds. this is so you can do dt.timedelta(dateA - dateB). See gotchas higher up for the 900 thing.
  • timedelta.totalSeconds() - returns total seconds between the two times.
  • timedelta.str() returns analog of python's str(time), which is %H:%M:%S.%f

Dealing with UTC

py-datetime is timezone naive, but dates and times around daylight savings can get bit iffy. There are two functions that help mitigate that

  • dt.datetime.utc() - pass in JS datetime in UTC timezone or unix epoch. the datetime object will be marked as operating in UTC
  • dt.datetime.strptime - if you pass in the date string in UTC, make sure to specify the third optional param to true. from there on out this date object will be marked as operating in UTC.

Be mindful around datemath, e.g. z = dt.datetime(dt.datetime.utc(someDate) + dt.timedelta(1)) will lose the UTC information, so you should do z = dt.datetime.utc(dt.datetime.utc(someDate) + dt.timedelta(1)). The API is bit sucky at the moment, so if you have any good ideas - pull requests welcome!