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

dateformat-util

v1.0.5

Published

A super-lightweight time conversion tool class

Downloads

24

Readme

dateformat.js

dateformat.js 是一个非常简洁、轻量级、不到 2kb 的很简洁的 Javascript 库, 它是一个时间的处理工具类。

  • 支持常用的时间格式化
  • 得到当前星期,时间对比大小,是否为闰年
  • 增加日期,增加月份,增加年份等等
  • 支持自动实时更新;
  • 支持浏览器script方式;
  • 测试用例完善,执行良好;

使用方法

1. 引入 timeago.js

通过 script 标签引入到html文件中,会生成一个全局变量 DateFormat.

<script src="dist/timeago.min.js"></script>

当然你还可以在通过 import 引入

import dateformat form '...自己的路径';

最后该项目也上传到npm上,你可以通过

//安装
npm install dateformat  
//使用
var dateformat = require('dateformat');
var df = new dateformat();
df.format(new Date());

3. 使用 dateformat

直接调用方法即可

DateFormat.format(new Date(), 'yyyy-MM-dd hh:mm:ss');

具体方法

1. format(date, fmt)

format 是将 Date类型的时间进行格式化的工具方法:

DateFormat.format(new Date()) //不传 fmt,则默认为 yyyy-MM-dd hh:mm:ss ;
DateFormat.format(new Date(), 'yyyy-MM-dd hh:mm:ss');
DateFormat.format(new Date(), 'yyyy/MM/dd hh:mm:ss');
DateFormat.format(new Date(), 'yyyy/MM/dd');
...

2. formatToDate(dateStr)

formatToDate 是将 字符串类型的时间 转化成 Date 类型的工具方法:

DateFormat.formatToDate('2017-04-18 12:12:12');
DateFormat.formatToDate('2017/04/18 12:12:12');
...

3. getDateStart(date)

getDateStart 是得到一天的开始,工作中会碰到这种需要得到某一天的开始或结束的时间点。

DateFormat.getDateStart(new Date());  //今天是4.18, 那返回的是今天 00:00:00的Date类型

如果想返回字符串类型,可以使用 getDateStartStr(date, fmt) 方法,fmt非必传

DateFormat.getDateStartStr(new Date(), 'yyyy-MM-dd hh:mm:ss'); //返回 2017-04-18 00:00:00

4. getDateEnd(date)

getDateEnd 是得到一天的结束

DateFormat.getDateEnd(new Date());  //今天是4.18, 那返回的是今天 23:59:59的Date类型

如果想返回字符串类型,可以使用 getDateEndStr(date, fmt) 方法,fmt非必传

DateFormat.getDateEndStr(new Date(), 'yyyy-MM-dd hh:mm:ss'); //返回 2017-04-18 23:59:59

5. compareDate(d1, d2)

compareDate 比较两个日期的大小 返回 1 , 则 d1 > d2 返回 0 , 则 d1 == d2 返回 -1, 则 d1 < d2

DateFormat.compareDate(new Date(), new Date()); 

6. getWeek(date, type)

getWeek 得到一个日期是星期几

DateFormat.getWeek(new Date());  //type非必传,默认返回 '星期 X'的格式
DateFormat.getWeek(new Date(), DateFormat.WEEKTYPE.US_DAYNAME); // Monday


/*
type 枚举如下:

DateFormat.WEEKTYPE.ZH_DAYNAME :      星期一
DateFormat.WEEKTYPE.ZH_SHORTDAYNAME:  周一
DateFormat.WEEKTYPE.US_DAYNAME:      Monday
DateFormat.WEEKTYPE.US_SDAYNAME:     Mon
*/

7. addDay(date, num)

增加系列:

addDay(date, num) 
addDayStr(dateStr, num) 
addMonth(date, num)
addMonthStr(dateStr, num)
addYear(date, num)
addYearStr(dateStr, num)

使用起来很简单,今天是 2017.04.18

DateFormat.addDay(new Date(), 3);   //返回是三天后的  Date类型
DateFormat.addDayStr('2017-04-18 12:12:12', 3) // 返回 Fri Apr 21 2017 12:12:12 GMT+0800 (中国标准时间)

// 其他 month year的方式同上