dart_ms

dart_ms is a Dart utility library for converting human-readable time strings into milliseconds. It is inspired by the popular ms package from the Node.js ecosystem.

Features

  • Convert time strings like 2 days, 1h, 30m to milliseconds.
  • Supports a wide range of time units including milliseconds, seconds, minutes, hours, days, weeks, and years.

Installation

Add dart_ms to your pubspec.yaml file:

dependencies:
  dart_ms: latest

Then, run pub get to install the package.

Usage

Import the library:

import 'package:dart_ms/dart_ms.dart';

Example

void main() {
  print(ms('2 days'));      // 172800000.0
  print(ms('1h'));          // 3600000.0
  print(ms('30m'));         // 1800000.0
  print(ms('45s'));         // 45000.0
  print(ms('500ms'));       // 500.0
  print(ms('1.5h'));        // 5400000.0
  print(ms('2.5 days'));    // 216000000.0
}

API

double? ms(String input)

Parses the given input string and returns the equivalent time in milliseconds.

  • input: A human-readable string representing time (e.g., 1h, 2 days, 30m).
  • Returns: A double representing the time in milliseconds, or null if the input is invalid.

Supported Units

  • milliseconds, msecs, ms
  • seconds, secs, s
  • minutes, mins, m
  • hours, hrs, h
  • days, d
  • weeks, w
  • years, yrs, y

Error Handling

If the input string contains an unsupported unit or is malformed, the ms function will throw an ArgumentError.

Libraries

dart_ms
Millisecond conversion utility library inspired by ms.