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

timespan-js

v0.1.1

Published

A javascript libaray witch can support humanized dispay of the timespan.

Downloads

9

Readme

#timespanjs ##简介 实现日期差值的人性化显示的javascript库。比如,可以显示成:2年2个月2小时4分钟23秒。同时支持国际化,可扩展。已经支持的语言:

  • 中文简体(zh-cn)
  • 中文繁体(zh-tw)
  • 英文(en)

###如何使用

  1. 通过script标签引入timespanjs脚本文件。如果使用requirejs,则需要通过require.config配置timespan路径。
  2. 创建Timespan对象。可以通过构造函数中传递时间差值创建,也可以通过传递两个Date对象实例创建。
  3. 调用humanize实例方法,获取人性化显示字符串。
  4. 调用Timespan.lang(),设置语言。

####浏览器下的引用

<script src="timespan.js"></script>
<script>
    var ts=new Timespan(85,'m');
</script>

####requirejs下的引用

require.config({
    paths: {
        "timespanjs": "path/to/timespan",
    }
});
define(["timespanjs"], function (Timespan) {
    var ts=new Timespan(85,'m');
});

####node.js下的引用 安装nodejs,通过npm命令安装timespanjs(注意:由于timespanjs被抢注,这里采用timespan-js作为nodejs中的包名)。npm地址:timespan-js

npm install timespan-js

调用方法:

var Timespan=require("timespan-js");
var ts=new Timespan(85,"m");
console.log(ts.humanize());

####调用示例:

//该值表示为:3小时4分钟25秒30毫秒
var msvalue = 30 + 1000 * 25 + 1000 * 60 * 4 + 1000 * 60 * 60 * 3;
var ts = new Timespan(msvalue, 'ms');
console.log(ts.humanize());//输出为:3小时4分钟25秒30毫秒

//通过fromDates方法构造
var dt=new Date(2014,7,1);
var dt1=new Date(2014,8,1,10,12,15,234);
var ts = Timespan.fromDates(dt, dt1);
console.log(ts.humanize());
//输出:1个月1天10小时12分钟15秒,234毫秒未输出,因为baseUnit参数默认是:'s'
Timespan.lang('en');
console.log(ts.humanize());
//输出:1 month,1 day,10 hours,12 minutes,15 seconds

##API说明 见api说明。 ##测试用例 见https://github.com/houyhea/timespanjs/blob/master/test/testTimespan.html。请获取并运行即可。 ##浏览器兼容性 兼容IE8+,chrome,firefox。 ##依赖 不需要依赖其他库。 ##协议 采用MIT 许可协议。 ##帮助 支付宝赞助(houyhea):
赞助