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

jlocal

v1.3.24

Published

js日期时间函数库,仿java8LocalDate的API设计

Downloads

53

Readme

JLocal

这是一个jsDate函数库,提供对Date和DateTime的基础操作。

使用

npm install jlocal
import {LocalDate,LocalDateTime,Period} from "jlocal";

基本操作

alert(LocalDate.of(2018,1,2).plusMonth(2).plusDay(22)); 
//alert 2018-03-24

//格式化
new LocalDateTime("2018-01-01 12:21:33").format("yyyy年MM月dd日");//默认为yyyy-MM-dd

JLocal采用ES6的语法编写,由三个类构成

LocalDate

构造对象

一个LocalDate对象仅拥有年、月、日

let localDate;
localDate = new LocalDate(new Date())
localDate = new LocalDate(new Date().getTime())
localDate = new LocalDate("2017-03-22")
// 静态方法
localDate = LocalDate.of(2017,3,22)

获取或设置日期

这些操作会改变localDate对象

localDate.year = 1018
localDate.month = 11
localDate.day = 24
localDate.week = 1 //设置成本周的第一天
// 不管年月日赋值如何 都会正确的计算

加减法

这些函数会返回一个新的LocalDate对象,而不会改变现有的对象

localDate.plusDay(20) //往后20天
localDate.minusDay(10)//往后10天
localDate.plusMonth(2);
localDate.plusWeek(1);
localDate.plusYear(1);
//省略减法

直接加上一个周期

localDate.plus(Period.between(localDate,new LocalDate()))
localDate.minus(Period.between(localDate,new LocalDate())) 

常用的操作

let n=1;
//获取本周的第n天 返回新的LocalDate
localDate.withDayOfWeek(n);
//更简单的 还有
localDate.firstDayOfWeek();
localDate.lastDayOfWeek();

//获取本月的第n天 返回新的LocalDate
localDate.withDayOfMonth(n);
localDate.firstDayOfMonth();
localDate.lastDayOfMonth();

//获取本年的第n天 返回新的LocalDate
localDate.withDayOfWeek(n);
localDate.firstDayOfYear();
localDate.lastDayOfYear();

//是否是闰年
localDate.isLeapYear();
//这个月有多少天
localDate.maxDayOfMonth();
//年
localDate.maxDayOfYear();

LocalDateTime

构造对象

LocalDateTime 相对 LocalDate 来说,多了时分秒和毫秒

let localDateTime = new LocalDateTime(new Date());
localDateTime = new LocalDateTime("2017-01-10 11:22:33.444");
localDateTime = LocalDateTime.of(2018,1,3,17,12,5,999)
localDateTime = new LocalDateTime(localDate);//将LocalDate扩展成LocalDateTime
//当然localDateTime 也可以转换成LocalDate
localDate = localDateTime.toLocalDate();
localDateTime = new LocalDateTime(localDateTime.toLocalDate())
//注意: 这样不会保留时分秒,以及毫秒

与LocalDate对比

LocalDateTime 继承了 LocalDate 的所有功能,当然也多出了对时分秒和毫秒的操作

let localDateTime = new LocalDateTime(new Date());
localDateTime.plusDay(1024).plusMilliseconds(1111).minusHours(3);
localDateTime.year = 2017;//改变了localDateTime 的年
localDateTime.hours += 1;//往后一小时

Period

Period用于描述两个时间之间的差let period=Perioid.between(start,end)
也可以方便计算时间LocalDateLocalDateTime对象都可以使用plus(period)minus(period)方法。

let period = Period.of(0,1,10,3);
alert(new LocalDateTime().plus(period));// 当前时间往后1月10天3小时

不同的时间单位描述

let period = Period.of(0,1,10,3);
period.getMonths() //相差多少个月 22
period.getDays() //相差多少天
//...
period.getMilliseconds()
//已上换算都是向下取整

例子

let a = new LocalDateTime("2017-02-01 12:00:00");
let b = new LocalDateTime("2016-02-08 13:00:00");
b.plus(Period.between(a,b)).equals(a);// return true

注意:period的总时间永远会大于0,如果要得知a和b的大小关系可以使用Period.between(a,b).isNegative?'a大':'b大; 是否等于Period.between(a,b).isEmpty


如果您有任何建议或意见,请提issues