Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
timestring
Advanced tools
The timestring npm package is a utility for parsing human-readable time strings into milliseconds. It allows users to convert strings like '2 days', '3 hours', or '5 minutes' into a numerical representation of time in milliseconds, which can be useful for scheduling, time calculations, and more.
Parse time strings to milliseconds
This feature allows you to convert a human-readable time string into milliseconds. For example, '2 days' is converted to 172800000 milliseconds.
const timestring = require('timestring');
const milliseconds = timestring('2 days'); // returns 172800000
Support for various time units
The package supports parsing of various time units such as seconds, minutes, hours, days, weeks, months, and years. It can handle combinations of these units in a single string.
const timestring = require('timestring');
const milliseconds = timestring('3 hours 15 minutes'); // returns 11700000
The ms package is a utility for converting time strings to milliseconds and vice versa. It is similar to timestring in that it can parse strings like '2 days' or '3h' into milliseconds. However, ms also provides functionality to convert milliseconds back into a human-readable format, which timestring does not offer.
The parse-duration package is another tool for parsing human-readable time strings into milliseconds. It offers similar functionality to timestring but includes additional features such as custom time unit definitions and more flexible parsing options.
Parse a human readable time string into a time based value.
npm install --save timestring
const timestring = require('timestring')
let str = '1h 15m'
let time = timestring(str)
console.log(time) // will log 4500
By default the returned time value from timestring
will be in seconds.
The time string can contain as many time groups as needed:
const timestring = require('timestring')
let str = '1d 3h 25m 18s'
let time = timestring(str)
console.log(time) // will log 98718
and can be as messy as you like:
const timestring = require('timestring')
let str = '1 d 3HOurS 25 min 1 8s'
let time = timestring(str)
console.log(time) // will log 98718
timestring
will parse the following keywords into time values:
ms, milli, millisecond, milliseconds
- will parse to millisecondss, sec, secs, second, seconds
- will parse to secondsm, min, mins, minute, minutes
- will parse to minutesh, hr, hrs, hour, hours
- will parse to hoursd, day, days
- will parse to daysw, week, weeks
- will parse to weeksmon, mth, mths, month, months
- will parse to monthsy, yr, yrs, year, years
- will parse to yearsKeywords can be used interchangeably:
const timestring = require('timestring')
let str = '1day 15h 20minutes 15s'
let time = timestring(str)
console.log(time) // will log 141615
By default the return time value will be in seconds. This can be changed by passing one of the following strings as an argument to timestring
:
ms
- Millisecondss
- Secondsm
- Minutesh
- Hoursd
- Daysw
- Weeksmth
- Monthsy
- Yearsconst timestring = require('timestring')
let str = '22h 16m'
let hours = timestring(str, 'h')
let days = timestring(str, 'd')
let weeks = timestring(str, 'w')
console.log(hours) // will log 22.266666666666666
console.log(days) // will log 0.9277777777777778
console.log(weeks) // will log 0.13253968253968254
A few assumptions are made by default:
These options can be changed by passing an options object as an argument to timestring
.
The following options are configurable:
hoursPerDay
daysPerWeek
weeksPerMonth
monthsPerYear
daysPerYear
const timestring = require('timestring')
let str = '1d'
let opts = {
hoursPerDay: 1
}
let time = timestring(str, 'h', opts)
console.log(time) // will log 1
In the example above hoursPerDay
is being set to 1
. When the time string is being parsed, the return value is being specified as hours. Normally 1d
would parse to 24
hours (as by default there are 24 hours in a day) but because hoursPerDay
has been set to 1
, 1d
will now only parse to 1
hour.
This would be useful for specific application needs.
Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type 1d
i want 7.5 hours to be tracked. When they type 1w
i want 5 days to be tracked etc.
const timestring = require('timestring')
let opts = {
hoursPerDay: 7.5,
daysPerWeek: 5
}
let hoursToday = timestring('1d', 'h', opts)
let daysThisWeek = timestring('1w', 'd', opts)
console.log(hoursToday) // will log 7.5
console.log(daysThisWeek) // will log 5
It is important to note that the daysPerYear
configuration option will be used to convert a month or year to seconds, so if you are using custom configuration options make sure that you adjust this value to suit if you expect to be parsing timestrings containing months or years.
If the string that is passed into timestring
can not be parsed then an error will be thrown:
const timestring = require('timestring')
let str = 'aaabbbccc'
let time = timestring(str) // will throw an error
If a number is passed into timestring
it will be treated as a string containing milliseconds:
const timestring = require('timestring')
let time = timestring(3000) // 3s
FAQs
Parse a human readable time string into a time based value
The npm package timestring receives a total of 180,367 weekly downloads. As such, timestring popularity was classified as popular.
We found that timestring demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.