🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

reltime

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reltime

A function to calculate relative time based on a simple date string notation. Intended to be lighter weight than the excellent moment.js.

Source
npmnpm
Version
0.0.0
Version published
Weekly downloads
23
-4.17%
Maintainers
1
Weekly downloads
 
Created
Source

reltime

A light weight relative time notation parser (e.g. 1d 2h, 3m, 4s is rendered as a relative date).

Overview and rationale

When building command line utilities I occasionally need to express time in a simple relative notation of day, hours, minutes and seconds (e.g. 1d 2h 3m 4s). There is a good, full featured time/date module called moment but it is also much more than what I need. That lead to reltime.js. It is a very simple relative time notation parser that only works on days, hours, minutes and seconds. It's less than 100 lines unminified.

	23 hours 4 minutes 2 seconds
	-23h 5m 6s

This is particularly helpful with command line utilities where you might have a parameter to process a relative time to the present. E.g.

	node mytool.js --update-since="-17m -2s"

Using moment.js to parse this one simple phrase just seems like overkill to me so that is why I've written reltime.js. It has three methods -

  • parse(JS_DATE_OBJECT, SIMPLE_TIME_STRING) - parse a relative time string
  • valueOf() - return the equivalent of JavaScript's Date's valueOf()
  • toString() - return the equivalent of JavaScript's Date's toString()

Example

Task

Compute the relative time of one hour, three minutes and ten seconds in the future:

Code

	var reltime = require("reltime");
	
	console.log("Show one hour, three minutes and ten seconds in the future",
		reltime.parse(new Date(), "1h 3m 10s").toString());

Task

Calculate the time 45 days ago.

Code

	var reltime = require("reltime");
	
	console.log("45 days ago", reltime.parse(new Date(), "-45d").toString());

Keywords

date

FAQs

Package last updated on 07 Dec 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts