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

parse-expire-datetime

v1.0.1

Published

parse string for expire datetime

Downloads

2

Readme

parse-expire-datetime

parse string for expire datetime

Install

npm install parse-expire-datetime

Format


Expanding mode
	Expand the hh/mm/ss/sss to 23/59/59/999, according timezone.

	format 1:
		YYYY-MM-DD( hh(:mm(:ss(.sss)?)?)?)?
			as YYYY-MM-DD (hh|23):(mm|59):(ss|59).(sss|999)

	format 2:
		YYYY-MM
			as YYYY-MM-(last-day-of-month) 23:59:59.999
	
	format 3:
		dd | dd hh | dd? hh:(mm(:ss(.sss)?)?)?
			as now + ( dd (hh|00):(mm|00):(ss|00).(sss|000) ), 
			then set hh/mm/ss/sss to 23/59/59/999 if they are omitted.

Adding mode
	format 4:
		ddD? hhH? mmM? ss.sssS?
			as now + ( dd (hh|00):(mm|00):(ss|00).(sss|000) ).

Usage & Api


var parse_expire_datetime = require("../parse-expire-datetime.js");
var { parseExpireDatetime } = parse_expire_datetime;

//Date to "YYYY-MM-DD hh:mm:ss.fff"
var toString23 = function (dt, toUtc) {...}

cmp = function (str, localExpect, options) {
	/*
	Parse string to expire datetime.

	parseExpireDatetime(str, options | timezoneOffset | now )
		options
			timezoneOffset
				Number type, in minutes, refer to Date.prototype.getTimezoneOffset().

			now
				A Date object, for debug purpose, only available for offset type string.

	Return a date object, or empty if fail.
	*/	
	var dt = parseExpireDatetime(str, options);
	if (!dt) {
		console.error("fail to parse, " + str);
		return false;	//format fail
	}
	if (toString23(dt) === localExpect) return true;
}

var dtNow = new Date(2022, 0, 1, 10, 20, 30, 40);	//for debug, 2022-01-01 10:20:30.040

done(!(
	// YYYY-MM-DD( hh(:mm(:ss(.sss)?)?)?)?, as YYYY-MM-DD (hh|23):(mm|59):(ss|59).(sss|999)
	cmp("2000-1-2 12:34:56.78", "2000-01-02 12:34:56.078") &&
	cmp("2000-1-2 12:34:56", "2000-01-02 12:34:56.999") &&
	cmp("2000-1-2 12:34", "2000-01-02 12:34:59.999") &&
	cmp("2000-1-2 12", "2000-01-02 12:59:59.999") &&
	cmp("2000-1-2", "2000-01-02 23:59:59.999") &&

	cmp("2000-1-2", "2000-01-02 22:59:59.999",
		{ timezoneOffset: (new Date()).getTimezoneOffset() - 60 }) &&		//timezoneOffset

	parseExpireDatetime("2000-01-01 00:00:00.000", 0).getTime() ===		//UTC+0
	Date.UTC(2000, 0, 1, 0, 0, 0, 0) &&

	parseExpireDatetime("2000-01-01 00:00:00.000", 0).getTime() ===		//UTC+0
	parseExpireDatetime("2000-01-01 08:00:00.000", -480).getTime() &&	//UTC+8

	// YYYY-MM, as YYYY-MM-(last-day-of-month) 23:59:59.999	
	cmp("2000-1", "2000-01-31 23:59:59.999") &&
	cmp("2000-2", "2000-02-29 23:59:59.999") &&
	cmp("2001-2", "2001-02-28 23:59:59.999") &&

	cmp("2001-2", "2001-02-28 22:59:59.999",
		(new Date()).getTimezoneOffset() - 60) &&	//timezoneOffset

	parseExpireDatetime("2001-2", 0).getTime() ===		//UTC+0
	Date.UTC(2001, 1, 28, 23, 59, 59, 999) &&
	parseExpireDatetime("2001-2", -480).getTime() ===		//UTC+0
	parseExpireDatetime("2001-2-28 15:59:59.999", 0).getTime() &&	//UTC+8

	// dd | dd hh | dd? hh:(mm(:ss(.sss)?)?)?, as now + ( dd (hh|00):(mm|00):(ss|00).(sss|000) ), then set hh/mm/ss/sss to 23/59/59/999 if they are omitted.
	cmp("1 3:4:5.6", "2022-01-02 13:24:35.046", { now: dtNow }) &&
	cmp("1 3:4:5", "2022-01-02 13:24:35.999", dtNow) &&
	cmp("1 3:4", "2022-01-02 13:24:59.999", dtNow) &&
	cmp("1 3", "2022-01-02 13:59:59.999", dtNow) &&
	cmp("1", "2022-01-02 23:59:59.999", dtNow) &&

	cmp("1 3:4", "2022-01-02 13:24:59.999",
		{ now: dtNow, timezoneOffset: (new Date()).getTimezoneOffset() - 60 }) &&
	cmp("1 3", "2022-01-02 13:59:59.999",
		{ now: dtNow, timezoneOffset: 0 }) &&	//UTC+0
	cmp("1 3", "2022-01-02 13:59:59.999",
		{ now: dtNow, timezoneOffset: -480 }) &&	//UTC+8
	cmp("1 3", "2022-01-02 13:59:59.999",
		{ now: dtNow, timezoneOffset: -300 }) &&	//UTC+5
	cmp("1 3", "2022-01-02 13:29:59.999",	//59-30=29
		{ now: dtNow, timezoneOffset: -330 }) &&	//UTC+5.5
	cmp("1 3:4", "2022-01-02 13:24:59.999",
		{ now: dtNow, timezoneOffset: -330 }) &&	//UTC+5.5

	cmp("3:4:5.6", "2022-01-01 13:24:35.046", dtNow) &&
	cmp("3:4:5", "2022-01-01 13:24:35.999", dtNow) &&
	cmp("3:4", "2022-01-01 13:24:59.999", dtNow) &&
	cmp("3:", "2022-01-01 13:59:59.999", dtNow) &&

	cmp("3:124", "2022-01-01 15:24:59.999", dtNow) &&	//value out of range

	cmp("3:4", "2022-01-01 13:24:59.999",
		{ now: dtNow, timezoneOffset: 0 }) &&	//UTC+0
	cmp("3:", "2022-01-01 13:59:59.999",
		{ now: dtNow, timezoneOffset: 0 }) &&	//UTC+0
	cmp("3:", "2022-01-01 13:59:59.999",
		{ now: dtNow, timezoneOffset: -480 }) &&	//UTC+8
	cmp("3:", "2022-01-01 13:29:59.999",	//59-30=29
		{ now: dtNow, timezoneOffset: -330 }) &&	//UTC+5.5
	cmp("3:4", "2022-01-01 13:24:59.999",
		{ now: dtNow, timezoneOffset: -330 }) &&	//UTC+5.5

	// ddD? hhH? mmM? ss.sssS?, as now + ( dd (hh|00):(mm|00):(ss|00).(sss|000) ).
	cmp("1d3h4m5.6s", "2022-01-02 13:24:35.046", dtNow) &&
	cmp("1d3h4m5s", "2022-01-02 13:24:35.040", dtNow) &&
	cmp("1d3h4m", "2022-01-02 13:24:30.040", dtNow) &&
	cmp("1d3h", "2022-01-02 13:20:30.040", dtNow) &&
	cmp("1d", "2022-01-02 10:20:30.040", dtNow) &&

	cmp("3h", "2022-01-01 13:20:30.040", dtNow) &&
	cmp("4m", "2022-01-01 10:24:30.040", dtNow) &&
	cmp("5s", "2022-01-01 10:20:35.040", dtNow) &&
	cmp("0.6s", "2022-01-01 10:20:30.046", dtNow) &&
	cmp(".6s", "2022-01-01 10:20:30.046", dtNow) &&

	cmp(" 1D   3H4m    5.6s    ", "2022-01-02 13:24:35.046", dtNow) &&	//spaces & cases

	cmp(" 1D   4m ", "2022-01-02 10:24:30.040", dtNow) &&	//partial
	cmp(" 3H    5s    ", "2022-01-01 13:20:35.040", dtNow) &&
	cmp(" 3H    .6s    ", "2022-01-01 13:20:30.046", dtNow) &&

	true
));